mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	feat(in_app_help): remove help notes that are no longer present in structure
This commit is contained in:
		| @@ -6,7 +6,7 @@ import noteService from "./notes.js"; | |||||||
| import log from "./log.js"; | import log from "./log.js"; | ||||||
| import migrationService from "./migration.js"; | import migrationService from "./migration.js"; | ||||||
| import { t } from "i18next"; | import { t } from "i18next"; | ||||||
| import { getHelpHiddenSubtreeData } from "./in_app_help.js"; | import { cleanUpHelp, getHelpHiddenSubtreeData } from "./in_app_help.js"; | ||||||
| import buildLaunchBarConfig from "./hidden_subtree_launcherbar.js"; | import buildLaunchBarConfig from "./hidden_subtree_launcherbar.js"; | ||||||
|  |  | ||||||
| const LBTPL_ROOT = "_lbTplRoot"; | const LBTPL_ROOT = "_lbTplRoot"; | ||||||
| @@ -58,7 +58,7 @@ enum Command { | |||||||
|  |  | ||||||
| let hiddenSubtreeDefinition: HiddenSubtreeItem; | let hiddenSubtreeDefinition: HiddenSubtreeItem; | ||||||
|  |  | ||||||
| function buildHiddenSubtreeDefinition(): HiddenSubtreeItem { | function buildHiddenSubtreeDefinition(helpSubtree: HiddenSubtreeItem[]): HiddenSubtreeItem { | ||||||
|     const launchbarConfig = buildLaunchBarConfig(); |     const launchbarConfig = buildLaunchBarConfig(); | ||||||
|  |  | ||||||
|     return { |     return { | ||||||
| @@ -283,7 +283,7 @@ function buildHiddenSubtreeDefinition(): HiddenSubtreeItem { | |||||||
|                 title: t("hidden-subtree.user-guide"), |                 title: t("hidden-subtree.user-guide"), | ||||||
|                 type: "book", |                 type: "book", | ||||||
|                 icon: "bx-help-circle", |                 icon: "bx-help-circle", | ||||||
|                 children: getHelpHiddenSubtreeData(), |                 children: helpSubtree, | ||||||
|                 isExpanded: true |                 isExpanded: true | ||||||
|             } |             } | ||||||
|         ] |         ] | ||||||
| @@ -301,11 +301,19 @@ function checkHiddenSubtree(force = false, extraOpts: CheckHiddenExtraOpts = {}) | |||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     const helpSubtree = getHelpHiddenSubtreeData(); | ||||||
|     if (!hiddenSubtreeDefinition || force) { |     if (!hiddenSubtreeDefinition || force) { | ||||||
|         hiddenSubtreeDefinition = buildHiddenSubtreeDefinition(); |         hiddenSubtreeDefinition = buildHiddenSubtreeDefinition(helpSubtree); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     checkHiddenSubtreeRecursively("root", hiddenSubtreeDefinition, extraOpts); |     checkHiddenSubtreeRecursively("root", hiddenSubtreeDefinition, extraOpts); | ||||||
|  |  | ||||||
|  |     try { | ||||||
|  |         cleanUpHelp(helpSubtree); | ||||||
|  |     } catch (e) { | ||||||
|  |         // Non-critical operation should something go wrong. | ||||||
|  |         console.error(e); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| function checkHiddenSubtreeRecursively(parentNoteId: string, item: HiddenSubtreeItem, extraOpts: CheckHiddenExtraOpts = {}) { | function checkHiddenSubtreeRecursively(parentNoteId: string, item: HiddenSubtreeItem, extraOpts: CheckHiddenExtraOpts = {}) { | ||||||
|   | |||||||
| @@ -5,6 +5,8 @@ import type NoteMeta from "./meta/note_meta.js"; | |||||||
| import type { NoteMetaFile } from "./meta/note_meta.js"; | import type { NoteMetaFile } from "./meta/note_meta.js"; | ||||||
| import { fileURLToPath } from "url"; | import { fileURLToPath } from "url"; | ||||||
| import { isDev } from "./utils.js"; | import { isDev } from "./utils.js"; | ||||||
|  | import type BNote from "../becca/entities/bnote.js"; | ||||||
|  | import becca from "../becca/becca.js"; | ||||||
|  |  | ||||||
| export function getHelpHiddenSubtreeData() { | export function getHelpHiddenSubtreeData() { | ||||||
|     const srcRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), ".."); |     const srcRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), ".."); | ||||||
| @@ -98,3 +100,55 @@ function parseNoteMeta(noteMeta: NoteMeta, docNameRoot: string): HiddenSubtreeIt | |||||||
|  |  | ||||||
|     return item; |     return item; | ||||||
| } | } | ||||||
|  | /** | ||||||
|  |  * Iterates recursively through the help subtree that the user has and compares it against the definition | ||||||
|  |  * to remove any notes that are no longer present in the latest version of the help. | ||||||
|  |  * | ||||||
|  |  * @param helpDefinition the hidden subtree definition for the help, to compare against the user's structure. | ||||||
|  |  */ | ||||||
|  | export function cleanUpHelp(helpDefinition: HiddenSubtreeItem[]) { | ||||||
|  |     function getFlatIds(items: HiddenSubtreeItem | HiddenSubtreeItem[]) { | ||||||
|  |         const ids: (string | string[])[] = []; | ||||||
|  |         if (Array.isArray(items)) { | ||||||
|  |             for (const item of items) { | ||||||
|  |                 ids.push(getFlatIds(item)); | ||||||
|  |             } | ||||||
|  |         } else { | ||||||
|  |             if (items.children) { | ||||||
|  |                 for (const child of items.children) { | ||||||
|  |                     ids.push(getFlatIds(child)); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             ids.push(items.id); | ||||||
|  |         } | ||||||
|  |         return ids.flat(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     function getFlatIdsFromNote(note: BNote | null) { | ||||||
|  |         if (!note) { | ||||||
|  |             return []; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         const ids: (string | string[])[] = []; | ||||||
|  |  | ||||||
|  |         for (const subnote of note.getChildNotes()) { | ||||||
|  |             ids.push(getFlatIdsFromNote(subnote)); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         ids.push(note.noteId); | ||||||
|  |         return ids.flat(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     const definitionHelpIds = new Set(getFlatIds(helpDefinition)); | ||||||
|  |     const realHelpIds = getFlatIdsFromNote(becca.getNote("_help")); | ||||||
|  |  | ||||||
|  |     for (const realHelpId of realHelpIds) { | ||||||
|  |         if (realHelpId === "_help") { | ||||||
|  |             continue; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (!definitionHelpIds.has(realHelpId)) { | ||||||
|  |             becca.getNote(realHelpId)?.deleteNote(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user