Skip to content

Build your personal knowledge base with Trilium Notes

fix(edit-docs): reduce a help link to the target note id

Elian Doranb0881f869d1d
diff --git a/apps/edit-docs/src/utils.spec.ts b/apps/edit-docs/src/utils.spec.ts
index 1349f26bf7..631747cc91 100644
--- a/apps/edit-docs/src/utils.spec.ts
+++ b/apps/edit-docs/src/utils.spec.ts
@@ -36,6 +36,31 @@ describe("rewriteHelpLinks", () => {
expect(result).toContain("#root/_hidden/_options/_optionsCodeNotes");
});
+ it("reduces the note path the editor writes to the target ID alone", () => {
+ // What a link created in the editor looks like: a full path whose intermediate IDs exist
+ // only in the docs instance. An import rewrites the same link to `#root/<noteId>`, so
+ // without this the exported HTML alternated between the two forms.
+ const input = [
+ `<a class="reference-link" href="#root/pOsGYCXsbNQG/tC7s2alapj8V/R9pX4DGra2Vt">Sharing</a>`,
+ `<a class="reference-link" href="#root/jdjRLhLV3TtI/YjerxU7Aii8X">Troubleshooting</a>`
+ ].join(" ");
+ const expected = [
+ `<a class="reference-link" href="#root/_help_R9pX4DGra2Vt">Sharing</a>`,
+ `<a class="reference-link" href="#root/_help_YjerxU7Aii8X">Troubleshooting</a>`
+ ].join(" ");
+ expect(rewriteHelpLinks(input)).toBe(expected);
+ });
+
+ it("reduces a path whose target is already prefixed, as a previously exported link is", () => {
+ const input = `<a href="#root/pOsGYCXsbNQG/KSZ04uQ2D1St/_help_hrZ1D00cLbal">Internal links</a>`;
+ expect(rewriteHelpLinks(input)).toBe(`<a href="#root/_help_hrZ1D00cLbal">Internal links</a>`);
+ });
+
+ it("keeps a query suffix on the link it rewrites", () => {
+ const input = `<a href="#root/pOsGYCXsbNQG/R9pX4DGra2Vt?viewMode=source">Sharing</a>`;
+ expect(rewriteHelpLinks(input)).toBe(`<a href="#root/_help_R9pX4DGra2Vt?viewMode=source">Sharing</a>`);
+ });
+
it("is idempotent for already-prefixed help links", () => {
const input = `<a href="#root/_help_iPIMuisry3hd">Text</a>`;
expect(rewriteHelpLinks(input)).toBe(input);
diff --git a/apps/edit-docs/src/utils.ts b/apps/edit-docs/src/utils.ts
index b1b6ab6249..329a49f267 100644
--- a/apps/edit-docs/src/utils.ts
+++ b/apps/edit-docs/src/utils.ts
@@ -175,21 +175,28 @@ function waitForEnd(archive: Archiver, stream: WriteStream) {
*
* In the edit-docs instance the help notes carry plain, randomly generated IDs, but in
* production they live under the `_help` subtree with a `_help_` prefix. This adds that
- * prefix to the link's target note ID.
+ * prefix to the link's target note ID, and reduces the link to that ID alone: the editor
+ * writes a full note path when a link is created, whose intermediate IDs belong to the
+ * docs instance and match nothing in the tree a reader has, leaving the client to fall
+ * back to the note's own path. The importer writes the same single-ID form, so a note
+ * exported straight after an edit now matches one that has been through an import.
*/
export function rewriteHelpLinks(content: string): string {
- return content.replace(/href="[^"]*#root[a-zA-Z0-9_/]*\/([a-zA-Z0-9_]+)[^"]*"/g, (match, targetNoteId) => {
- // Canonical hidden-subtree notes (e.g. _options, _optionsTextNotes) keep their IDs in
- // production, so they already start with an underscore. Only help notes (random
- // alphanumeric IDs) get the `_help_` prefix; prefixing the others would produce broken
- // `_help__optionsTextNotes`-style links (see issue #9646).
- if (targetNoteId.startsWith("_")) {
- return match;
- }
- const components = match.split("/");
- components[components.length - 1] = `_help_${components[components.length - 1]}`;
- return components.join("/");
- });
+ return content.replace(/href="([^"]*#root)(?:[a-zA-Z0-9_/]*\/)?([a-zA-Z0-9_]+)([^"]*)"/g,
+ (match, prefix: string, targetNoteId: string, suffix: string) => {
+ if (targetNoteId.startsWith("_help_")) {
+ return `href="${prefix}/${targetNoteId}${suffix}"`;
+ }
+
+ // Canonical hidden-subtree notes (e.g. _options, _optionsTextNotes) keep their IDs in
+ // production, so they already start with an underscore, and their path is a real one.
+ // Only help notes (random alphanumeric IDs) get the `_help_` prefix; prefixing the
+ // others would produce broken `_help__optionsTextNotes`-style links (see issue #9646).
+ if (targetNoteId.startsWith("_")) {
+ return match;
+ }
+ return `href="${prefix}/_help_${targetNoteId}${suffix}"`;
+ });
}
export async function createZipFromDirectory(dirPath: string, zipPath: string) {