Skip to content

Build your personal knowledge base with Trilium Notes

add #toc label to control Table of Contents visibility per note, #2985

zadamf890e9917fb8
diff --git a/src/public/app/widgets/attribute_widgets/attribute_detail.js b/src/public/app/widgets/attribute_widgets/attribute_detail.js
index 4645d82113..e46625ad5d 100644
--- a/src/public/app/widgets/attribute_widgets/attribute_detail.js
+++ b/src/public/app/widgets/attribute_widgets/attribute_detail.js
@@ -227,7 +227,9 @@ const ATTR_HELP = {
<li><code>Log for \${now.format('YYYY-MM-DD HH:mm:ss')}</code></li>
</ul>
- See <a href="https://github.com/zadam/trilium/wiki/Default-note-title">wiki with details</a>, API docs for <a href="https://zadam.github.io/trilium/backend_api/Note.html">parentNote</a> and <a href="https://day.js.org/docs/en/display/format">now</a> for details.`
+ See <a href="https://github.com/zadam/trilium/wiki/Default-note-title">wiki with details</a>, API docs for <a href="https://zadam.github.io/trilium/backend_api/Note.html">parentNote</a> and <a href="https://day.js.org/docs/en/display/format">now</a> for details.`,
+ "template": "This note will appear in the selection of available template when creating new note",
+ "toc": "<code>#toc</code> or <code>#toc=show</code> will force the Table of Contents to be shown, <code>#toc=hide</code> will force hiding it. If the label doesn't exist, the global setting is observed"
},
"relation": {
"runOnNoteCreation": "executes when note is created on backend",
diff --git a/src/public/app/widgets/toc.js b/src/public/app/widgets/toc.js
index c127083f1f..22837a8094 100644
--- a/src/public/app/widgets/toc.js
+++ b/src/public/app/widgets/toc.js
@@ -74,9 +74,7 @@ export default class TocWidget extends CollapsibleWidget {
}
isEnabled() {
- return super.isEnabled()
- && this.note.type === 'text'
- && !this.note.hasLabel('noToc');
+ return super.isEnabled() && this.note.type === 'text';
}
async doRenderBody() {
@@ -85,6 +83,14 @@ export default class TocWidget extends CollapsibleWidget {
}
async refreshWithNote(note) {
+ const tocLabel = note.getLabel('toc');
+
+ if (tocLabel?.value === 'hide') {
+ this.toggleInt(false);
+ this.triggerCommand("reevaluateIsEnabled");
+ return;
+ }
+
let $toc = "", headingCount = 0;
// Check for type text unconditionally in case alwaysShowWidget is set
if (this.note.type === 'text') {
@@ -93,7 +99,11 @@ export default class TocWidget extends CollapsibleWidget {
}
this.$toc.html($toc);
- this.toggleInt(headingCount >= options.getInt('minTocHeadings'));
+ this.toggleInt(
+ ["", "show"].includes(tocLabel?.value)
+ || headingCount >= options.getInt('minTocHeadings')
+ );
+
this.triggerCommand("reevaluateIsEnabled");
}
@@ -241,7 +251,7 @@ export default class TocWidget extends CollapsibleWidget {
if (loadResults.isNoteContentReloaded(this.noteId)) {
await this.refresh();
} else if (loadResults.getAttributes().find(attr => attr.type === 'label'
- && (attr.name.toLowerCase().includes('readonly') || attr.name === 'noToc')
+ && (attr.name.toLowerCase().includes('readonly') || attr.name === 'toc')
&& attributeService.isAffecting(attr, this.note))) {
await this.refresh();
diff --git a/src/services/builtin_attributes.js b/src/services/builtin_attributes.js
index bda0f76547..814f1deafc 100644
--- a/src/services/builtin_attributes.js
+++ b/src/services/builtin_attributes.js
@@ -52,6 +52,7 @@ module.exports = [
{ type: 'label', name: 'hideRelations' },
{ type: 'label', name: 'titleTemplate', isDangerous: true },
{ type: 'label', name: 'template' },
+ { type: 'label', name: 'toc' },
// relation names
{ type: 'relation', name: 'internalLink' },