From 1a9d64e0cf9418bdbadf35e34feb7ee35a7b5f0b Mon Sep 17 00:00:00 2001 From: "leonardo.farias" Date: Mon, 16 Oct 2023 22:16:07 -0300 Subject: [PATCH 1/4] Fixing regex to not remove dashes - --- packages/remark-wiki-link/src/lib/fromMarkdown.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/remark-wiki-link/src/lib/fromMarkdown.ts b/packages/remark-wiki-link/src/lib/fromMarkdown.ts index 4f9036dff..50824c007 100644 --- a/packages/remark-wiki-link/src/lib/fromMarkdown.ts +++ b/packages/remark-wiki-link/src/lib/fromMarkdown.ts @@ -79,7 +79,7 @@ function fromMarkdown(opts: FromMarkdownOptions = {}) { data: { isEmbed, target, alias }, } = wikiLink; // eslint-disable-next-line no-useless-escape - const wikiLinkWithHeadingPattern = /([\p{Letter}\d\s\/\.-_]*)(#.*)?/u; + const wikiLinkWithHeadingPattern = /([\p{Letter}\d\s\/\.-_-]*)(#.*)?/u; const [, path, heading = ""] = target.match(wikiLinkWithHeadingPattern); const possibleWikiLinkPermalinks = wikiLinkResolver(path); From 737f880036df4c85b406cd3c32bbe3e7337716e3 Mon Sep 17 00:00:00 2001 From: "leonardo.farias" Date: Thu, 26 Oct 2023 00:00:06 -0300 Subject: [PATCH 2/4] Changed regex to permit any symbols other than # --- .changeset/old-planets-battle.md | 5 + .../remark-wiki-link/src/lib/fromMarkdown.ts | 2 +- packages/remark-wiki-link/src/lib/html.ts | 2 +- .../test/getPermalinks.spec.ts | 2 +- .../test/remarkWikiLink.spec.ts | 207 ++++++++++++++++-- 5 files changed, 199 insertions(+), 19 deletions(-) create mode 100644 .changeset/old-planets-battle.md diff --git a/.changeset/old-planets-battle.md b/.changeset/old-planets-battle.md new file mode 100644 index 000000000..f613b29c9 --- /dev/null +++ b/.changeset/old-planets-battle.md @@ -0,0 +1,5 @@ +--- +'@portaljs/remark-wiki-link': major +--- + +Changed regex to permit any symbols other than # diff --git a/packages/remark-wiki-link/src/lib/fromMarkdown.ts b/packages/remark-wiki-link/src/lib/fromMarkdown.ts index 50824c007..d9e9a0efd 100644 --- a/packages/remark-wiki-link/src/lib/fromMarkdown.ts +++ b/packages/remark-wiki-link/src/lib/fromMarkdown.ts @@ -79,7 +79,7 @@ function fromMarkdown(opts: FromMarkdownOptions = {}) { data: { isEmbed, target, alias }, } = wikiLink; // eslint-disable-next-line no-useless-escape - const wikiLinkWithHeadingPattern = /([\p{Letter}\d\s\/\.-_-]*)(#.*)?/u; + const wikiLinkWithHeadingPattern = /^(.*?)(#.*)?$/u; const [, path, heading = ""] = target.match(wikiLinkWithHeadingPattern); const possibleWikiLinkPermalinks = wikiLinkResolver(path); diff --git a/packages/remark-wiki-link/src/lib/html.ts b/packages/remark-wiki-link/src/lib/html.ts index d4a7996fb..021970a8e 100644 --- a/packages/remark-wiki-link/src/lib/html.ts +++ b/packages/remark-wiki-link/src/lib/html.ts @@ -64,7 +64,7 @@ function html(opts: HtmlOptions = {}) { const { target, alias } = wikiLink; const isEmbed = token.isType === "embed"; // eslint-disable-next-line no-useless-escape - const wikiLinkWithHeadingPattern = /([\w\s\/\.-]*)(#.*)?/; + const wikiLinkWithHeadingPattern = /^(.*?)(#.*)?$/u; const [, path, heading = ""] = target.match(wikiLinkWithHeadingPattern); const possibleWikiLinkPermalinks = wikiLinkResolver(path); diff --git a/packages/remark-wiki-link/test/getPermalinks.spec.ts b/packages/remark-wiki-link/test/getPermalinks.spec.ts index 1e1f51d30..de916171a 100644 --- a/packages/remark-wiki-link/test/getPermalinks.spec.ts +++ b/packages/remark-wiki-link/test/getPermalinks.spec.ts @@ -6,7 +6,7 @@ import { getPermalinks } from "../src/utils"; // const markdownFolder = path.join(__dirname, "/fixtures/content"); const markdownFolder = path.join( ".", - "/packages/remark-wiki-link/test/fixtures/content" + "test/fixtures/content" ); describe("getPermalinks", () => { diff --git a/packages/remark-wiki-link/test/remarkWikiLink.spec.ts b/packages/remark-wiki-link/test/remarkWikiLink.spec.ts index 6b4e9cc51..5c5ce5b3a 100644 --- a/packages/remark-wiki-link/test/remarkWikiLink.spec.ts +++ b/packages/remark-wiki-link/test/remarkWikiLink.spec.ts @@ -6,10 +6,14 @@ import { Node } from "unist"; import wikiLinkPlugin from "../src/lib/remarkWikiLink"; +function createMarkdownProcessorWithWikiLinkPlugin() { + return unified().use(markdown).use(wikiLinkPlugin); +} + describe("remark-wiki-link", () => { describe("parses a wikilink", () => { test("with 'raw' file format (default) that has no matching permalink", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("[[Wiki Link]]"); ast = processor.runSync(ast); @@ -161,7 +165,7 @@ describe("remark-wiki-link", () => { describe("aliases and headings", () => { test("parses a wiki link with heading", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("[[Wiki Link#Some Heading]]"); ast = processor.runSync(ast); @@ -185,8 +189,32 @@ describe("remark-wiki-link", () => { }); }); + test("Alias with accent", () => { + const processor = createMarkdownProcessorWithWikiLinkPlugin(); + + let ast = processor.parse("[[link|Alias with àcèôíã]]"); + ast = processor.runSync(ast); + expect(select("wikiLink", ast)).not.toEqual(null); + + visit(ast, "wikiLink", (node: Node) => { + expect(node.data?.exists).toEqual(false); + expect(node.data?.permalink).toEqual("link"); + expect(node.data?.alias).toEqual("Alias with àcèôíã"); + expect(node.data?.hName).toEqual("a"); + expect((node.data?.hProperties as any).className).toEqual( + "internal new" + ); + expect((node.data?.hProperties as any).href).toEqual( + "link" + ); + expect((node.data?.hChildren as any)[0].value).toEqual( + "Alias with àcèôíã" + ); + }) + }) + test("parses a wiki link with heading and alias", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("[[Wiki Link#Some Heading|Alias]]"); ast = processor.runSync(ast); @@ -209,7 +237,7 @@ describe("remark-wiki-link", () => { }); test("parses a wiki link to a heading on the same page", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("[[#Some Heading]]"); ast = processor.runSync(ast); @@ -228,11 +256,35 @@ describe("remark-wiki-link", () => { expect((node.data?.hChildren as any)[0].value).toEqual("Some Heading"); }); }); + + test("parses a link and alias with accent", () => { + const processor = createMarkdownProcessorWithWikiLinkPlugin(); + + let ast = processor.parse("[[link|Alias-with-dashes]]"); + ast = processor.runSync(ast); + expect(select("wikiLink", ast)).not.toEqual(null); + + visit(ast, "wikiLink", (node: Node) => { + expect(node.data?.exists).toEqual(false); + expect(node.data?.permalink).toEqual("link"); + expect(node.data?.alias).toEqual("Alias-with-dashes"); + expect(node.data?.hName).toEqual("a"); + expect((node.data?.hProperties as any).className).toEqual( + "internal new" + ); + expect((node.data?.hProperties as any).href).toEqual( + "link" + ); + expect((node.data?.hChildren as any)[0].value).toEqual( + "Alias-with-dashes" + ); + }) + }) }); describe("image embeds", () => { test("parses an image embed of supported file format", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("![[My Image.png]]"); ast = processor.runSync(ast); @@ -250,7 +302,7 @@ describe("remark-wiki-link", () => { }); test("parses an image embed of unsupported file format", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("![[My Image.xyz]]"); ast = processor.runSync(ast); @@ -324,7 +376,7 @@ describe("remark-wiki-link", () => { }); test("parses an image embed with an alt text", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("![[My Image.png|Alt Text]]"); ast = processor.runSync(ast); @@ -342,7 +394,7 @@ describe("remark-wiki-link", () => { }); test("parses a pdf embed", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("![[My Document.pdf]]"); ast = processor.runSync(ast); @@ -361,9 +413,131 @@ describe("remark-wiki-link", () => { }); }); + describe("Links with special characters", () => { + test("parses a link with accent", () => { + const processor = createMarkdownProcessorWithWikiLinkPlugin(); + + let ast = processor.parse("[[link with àcèôíã]]"); + ast = processor.runSync(ast); + expect(select("wikiLink", ast)).not.toEqual(null); + + visit(ast, "wikiLink", (node: Node) => { + expect(node.data?.exists).toEqual(false); + expect(node.data?.permalink).toEqual("link with àcèôíã"); + expect(node.data?.alias).toEqual(null); + expect(node.data?.hName).toEqual("a"); + expect((node.data?.hProperties as any).className).toEqual( + "internal new" + ); + expect((node.data?.hProperties as any).href).toEqual( + "link with àcèôíã" + ); + expect((node.data?.hChildren as any)[0].value).toEqual( + "link with àcèôíã" + ); + }) + }); + + test("parses a link with dashes", () => { + const processor = createMarkdownProcessorWithWikiLinkPlugin(); + + let ast = processor.parse("[[link-with-dashes]]"); + ast = processor.runSync(ast); + expect(select("wikiLink", ast)).not.toEqual(null); + + visit(ast, "wikiLink", (node: Node) => { + expect(node.data?.exists).toEqual(false); + expect(node.data?.permalink).toEqual("link-with-dashes"); + expect(node.data?.alias).toEqual(null); + expect(node.data?.hName).toEqual("a"); + expect((node.data?.hProperties as any).className).toEqual( + "internal new" + ); + expect((node.data?.hProperties as any).href).toEqual( + "link-with-dashes" + ); + expect((node.data?.hChildren as any)[0].value).toEqual( + "link-with-dashes" + ); + }) + }); + + test("parses a link with underline", () => { + const processor = createMarkdownProcessorWithWikiLinkPlugin(); + + let ast = processor.parse("[[link_with_dashes]]"); + ast = processor.runSync(ast); + expect(select("wikiLink", ast)).not.toEqual(null); + + visit(ast, "wikiLink", (node: Node) => { + expect(node.data?.exists).toEqual(false); + expect(node.data?.permalink).toEqual("link_with_dashes"); + expect(node.data?.alias).toEqual(null); + expect(node.data?.hName).toEqual("a"); + expect((node.data?.hProperties as any).className).toEqual( + "internal new" + ); + expect((node.data?.hProperties as any).href).toEqual( + "link_with_dashes" + ); + expect((node.data?.hChildren as any)[0].value).toEqual( + "link_with_dashes" + ); + }) + }); + + test("parses a link with parenthesis", () => { + const processor = createMarkdownProcessorWithWikiLinkPlugin(); + + let ast = processor.parse("[[(link wi(th) (p)arenthesis)]]"); + ast = processor.runSync(ast); + expect(select("wikiLink", ast)).not.toEqual(null); + + visit(ast, "wikiLink", (node: Node) => { + expect(node.data?.exists).toEqual(false); + expect(node.data?.permalink).toEqual("(link wi(th) (p)arenthesis)"); + expect(node.data?.alias).toEqual(null); + expect(node.data?.hName).toEqual("a"); + expect((node.data?.hProperties as any).className).toEqual( + "internal new" + ); + expect((node.data?.hProperties as any).href).toEqual( + "(link wi(th) (p)arenthesis)" + ); + expect((node.data?.hChildren as any)[0].value).toEqual( + "(link wi(th) (p)arenthesis)" + ); + }) + }); + + test("parses a link with random symbols", () => { + const processor = createMarkdownProcessorWithWikiLinkPlugin(); + + let ast = processor.parse("[[my file !:ª%@'*º$#°~./\\]]"); + ast = processor.runSync(ast); + expect(select("wikiLink", ast)).not.toEqual(null); + + visit(ast, "wikiLink", (node: Node) => { + expect(node.data?.exists).toEqual(false); + expect(node.data?.permalink).toEqual("my file !:ª%@'*º$"); + expect(node.data?.alias).toEqual(null); + expect(node.data?.hName).toEqual("a"); + expect((node.data?.hProperties as any).className).toEqual( + "internal new" + ); + expect((node.data?.hProperties as any).href).toEqual( + "my file !:ª%@'*º$#°~./\\" + ); + expect((node.data?.hChildren as any)[0].value).toEqual( + "my file !:ª%@'*º$#°~./\\" + ); + }) + }); + }) + describe("invalid wiki links", () => { test("doesn't parse a wiki link with two missing closing brackets", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("[[Wiki Link"); ast = processor.runSync(ast); @@ -372,7 +546,7 @@ describe("remark-wiki-link", () => { }); test("doesn't parse a wiki link with one missing closing bracket", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("[[Wiki Link]"); ast = processor.runSync(ast); @@ -381,7 +555,7 @@ describe("remark-wiki-link", () => { }); test("doesn't parse a wiki link with a missing opening bracket", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("Wiki Link]]"); ast = processor.runSync(ast); @@ -390,7 +564,7 @@ describe("remark-wiki-link", () => { }); test("doesn't parse a wiki link in single brackets", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("[Wiki Link]"); ast = processor.runSync(ast); @@ -434,7 +608,7 @@ describe("remark-wiki-link", () => { }); test("parses wiki links to index files", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("[[/some/folder/index]]"); ast = processor.runSync(ast); @@ -456,7 +630,7 @@ describe("remark-wiki-link", () => { describe("other", () => { test("parses a wiki link to some index page in a folder with no matching permalink", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("[[/some/folder/index]]"); ast = processor.runSync(ast); @@ -498,7 +672,7 @@ describe("remark-wiki-link", () => { }); test("parses a wiki link to home index page with no matching permalink", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("[[/index]]"); ast = processor.runSync(ast); @@ -538,7 +712,7 @@ describe("remark-wiki-link", () => { describe("transclusions", () => { test("replaces a transclusion with a regular wiki link", () => { - const processor = unified().use(markdown).use(wikiLinkPlugin); + const processor = createMarkdownProcessorWithWikiLinkPlugin(); let ast = processor.parse("![[Some Page]]"); ast = processor.runSync(ast); @@ -560,3 +734,4 @@ describe("remark-wiki-link", () => { }); }); }); + From 85bb6cb98c53bedc2add3d014927570b5dd1bbdf Mon Sep 17 00:00:00 2001 From: "leonardo.farias" Date: Fri, 27 Oct 2023 22:35:16 -0300 Subject: [PATCH 3/4] Changed tests and created tests to verify the generated prefix in the HTML plugin --- ...lanets-battle.md => many-ligers-sneeze.md} | 2 +- .../test/micromarkExtensionWikiLink.spec.ts | 11 + .../test/remarkWikiLink.spec.ts | 192 ++---------------- 3 files changed, 34 insertions(+), 171 deletions(-) rename .changeset/{old-planets-battle.md => many-ligers-sneeze.md} (61%) diff --git a/.changeset/old-planets-battle.md b/.changeset/many-ligers-sneeze.md similarity index 61% rename from .changeset/old-planets-battle.md rename to .changeset/many-ligers-sneeze.md index f613b29c9..499eeba75 100644 --- a/.changeset/old-planets-battle.md +++ b/.changeset/many-ligers-sneeze.md @@ -1,5 +1,5 @@ --- -'@portaljs/remark-wiki-link': major +'@portaljs/remark-wiki-link': patch --- Changed regex to permit any symbols other than # diff --git a/packages/remark-wiki-link/test/micromarkExtensionWikiLink.spec.ts b/packages/remark-wiki-link/test/micromarkExtensionWikiLink.spec.ts index a2c408c64..165717e76 100644 --- a/packages/remark-wiki-link/test/micromarkExtensionWikiLink.spec.ts +++ b/packages/remark-wiki-link/test/micromarkExtensionWikiLink.spec.ts @@ -321,4 +321,15 @@ describe("micromark-extension-wiki-link", () => { ); }); }); + + describe("Links with special characters", () => { + test("parses a link with special characters and symbols", () => { + const serialized = micromark("[[li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\]]", "ascii", { + extensions: [syntax()], + htmlExtensions: [html() as any], + }); + const prefixExpected = '

{ describe("parses a wikilink", () => { test("with 'raw' file format (default) that has no matching permalink", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("[[Wiki Link]]"); ast = processor.runSync(ast); @@ -165,7 +161,7 @@ describe("remark-wiki-link", () => { describe("aliases and headings", () => { test("parses a wiki link with heading", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("[[Wiki Link#Some Heading]]"); ast = processor.runSync(ast); @@ -189,32 +185,8 @@ describe("remark-wiki-link", () => { }); }); - test("Alias with accent", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); - - let ast = processor.parse("[[link|Alias with àcèôíã]]"); - ast = processor.runSync(ast); - expect(select("wikiLink", ast)).not.toEqual(null); - - visit(ast, "wikiLink", (node: Node) => { - expect(node.data?.exists).toEqual(false); - expect(node.data?.permalink).toEqual("link"); - expect(node.data?.alias).toEqual("Alias with àcèôíã"); - expect(node.data?.hName).toEqual("a"); - expect((node.data?.hProperties as any).className).toEqual( - "internal new" - ); - expect((node.data?.hProperties as any).href).toEqual( - "link" - ); - expect((node.data?.hChildren as any)[0].value).toEqual( - "Alias with àcèôíã" - ); - }) - }) - test("parses a wiki link with heading and alias", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("[[Wiki Link#Some Heading|Alias]]"); ast = processor.runSync(ast); @@ -237,7 +209,7 @@ describe("remark-wiki-link", () => { }); test("parses a wiki link to a heading on the same page", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("[[#Some Heading]]"); ast = processor.runSync(ast); @@ -256,35 +228,11 @@ describe("remark-wiki-link", () => { expect((node.data?.hChildren as any)[0].value).toEqual("Some Heading"); }); }); - - test("parses a link and alias with accent", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); - - let ast = processor.parse("[[link|Alias-with-dashes]]"); - ast = processor.runSync(ast); - expect(select("wikiLink", ast)).not.toEqual(null); - - visit(ast, "wikiLink", (node: Node) => { - expect(node.data?.exists).toEqual(false); - expect(node.data?.permalink).toEqual("link"); - expect(node.data?.alias).toEqual("Alias-with-dashes"); - expect(node.data?.hName).toEqual("a"); - expect((node.data?.hProperties as any).className).toEqual( - "internal new" - ); - expect((node.data?.hProperties as any).href).toEqual( - "link" - ); - expect((node.data?.hChildren as any)[0].value).toEqual( - "Alias-with-dashes" - ); - }) - }) }); describe("image embeds", () => { test("parses an image embed of supported file format", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("![[My Image.png]]"); ast = processor.runSync(ast); @@ -302,7 +250,7 @@ describe("remark-wiki-link", () => { }); test("parses an image embed of unsupported file format", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("![[My Image.xyz]]"); ast = processor.runSync(ast); @@ -376,7 +324,7 @@ describe("remark-wiki-link", () => { }); test("parses an image embed with an alt text", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("![[My Image.png|Alt Text]]"); ast = processor.runSync(ast); @@ -394,7 +342,7 @@ describe("remark-wiki-link", () => { }); test("parses a pdf embed", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("![[My Document.pdf]]"); ast = processor.runSync(ast); @@ -414,122 +362,26 @@ describe("remark-wiki-link", () => { }); describe("Links with special characters", () => { - test("parses a link with accent", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); - - let ast = processor.parse("[[link with àcèôíã]]"); - ast = processor.runSync(ast); - expect(select("wikiLink", ast)).not.toEqual(null); - - visit(ast, "wikiLink", (node: Node) => { - expect(node.data?.exists).toEqual(false); - expect(node.data?.permalink).toEqual("link with àcèôíã"); - expect(node.data?.alias).toEqual(null); - expect(node.data?.hName).toEqual("a"); - expect((node.data?.hProperties as any).className).toEqual( - "internal new" - ); - expect((node.data?.hProperties as any).href).toEqual( - "link with àcèôíã" - ); - expect((node.data?.hChildren as any)[0].value).toEqual( - "link with àcèôíã" - ); - }) - }); - - test("parses a link with dashes", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); - - let ast = processor.parse("[[link-with-dashes]]"); - ast = processor.runSync(ast); - expect(select("wikiLink", ast)).not.toEqual(null); - - visit(ast, "wikiLink", (node: Node) => { - expect(node.data?.exists).toEqual(false); - expect(node.data?.permalink).toEqual("link-with-dashes"); - expect(node.data?.alias).toEqual(null); - expect(node.data?.hName).toEqual("a"); - expect((node.data?.hProperties as any).className).toEqual( - "internal new" - ); - expect((node.data?.hProperties as any).href).toEqual( - "link-with-dashes" - ); - expect((node.data?.hChildren as any)[0].value).toEqual( - "link-with-dashes" - ); - }) - }); - - test("parses a link with underline", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); - - let ast = processor.parse("[[link_with_dashes]]"); - ast = processor.runSync(ast); - expect(select("wikiLink", ast)).not.toEqual(null); - - visit(ast, "wikiLink", (node: Node) => { - expect(node.data?.exists).toEqual(false); - expect(node.data?.permalink).toEqual("link_with_dashes"); - expect(node.data?.alias).toEqual(null); - expect(node.data?.hName).toEqual("a"); - expect((node.data?.hProperties as any).className).toEqual( - "internal new" - ); - expect((node.data?.hProperties as any).href).toEqual( - "link_with_dashes" - ); - expect((node.data?.hChildren as any)[0].value).toEqual( - "link_with_dashes" - ); - }) - }); - - test("parses a link with parenthesis", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); - - let ast = processor.parse("[[(link wi(th) (p)arenthesis)]]"); - ast = processor.runSync(ast); - expect(select("wikiLink", ast)).not.toEqual(null); - - visit(ast, "wikiLink", (node: Node) => { - expect(node.data?.exists).toEqual(false); - expect(node.data?.permalink).toEqual("(link wi(th) (p)arenthesis)"); - expect(node.data?.alias).toEqual(null); - expect(node.data?.hName).toEqual("a"); - expect((node.data?.hProperties as any).className).toEqual( - "internal new" - ); - expect((node.data?.hProperties as any).href).toEqual( - "(link wi(th) (p)arenthesis)" - ); - expect((node.data?.hChildren as any)[0].value).toEqual( - "(link wi(th) (p)arenthesis)" - ); - }) - }); - - test("parses a link with random symbols", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + test("parses a link with special characters and symbols", () => { + const processor = unified().use(markdown).use(wikiLinkPlugin); - let ast = processor.parse("[[my file !:ª%@'*º$#°~./\\]]"); + let ast = processor.parse("[[li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#li-nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\]]"); ast = processor.runSync(ast); expect(select("wikiLink", ast)).not.toEqual(null); visit(ast, "wikiLink", (node: Node) => { expect(node.data?.exists).toEqual(false); - expect(node.data?.permalink).toEqual("my file !:ª%@'*º$"); + expect(node.data?.permalink).toEqual("li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\"); expect(node.data?.alias).toEqual(null); expect(node.data?.hName).toEqual("a"); expect((node.data?.hProperties as any).className).toEqual( "internal new" ); expect((node.data?.hProperties as any).href).toEqual( - "my file !:ª%@'*º$#°~./\\" + "li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#li-nk-w(i)th-àcèô-íã_a(n)d_underline!:ª%@'*º$ °~./\\" ); expect((node.data?.hChildren as any)[0].value).toEqual( - "my file !:ª%@'*º$#°~./\\" + "li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#li-nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\" ); }) }); @@ -537,7 +389,7 @@ describe("remark-wiki-link", () => { describe("invalid wiki links", () => { test("doesn't parse a wiki link with two missing closing brackets", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("[[Wiki Link"); ast = processor.runSync(ast); @@ -546,7 +398,7 @@ describe("remark-wiki-link", () => { }); test("doesn't parse a wiki link with one missing closing bracket", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("[[Wiki Link]"); ast = processor.runSync(ast); @@ -555,7 +407,7 @@ describe("remark-wiki-link", () => { }); test("doesn't parse a wiki link with a missing opening bracket", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("Wiki Link]]"); ast = processor.runSync(ast); @@ -564,7 +416,7 @@ describe("remark-wiki-link", () => { }); test("doesn't parse a wiki link in single brackets", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("[Wiki Link]"); ast = processor.runSync(ast); @@ -608,7 +460,7 @@ describe("remark-wiki-link", () => { }); test("parses wiki links to index files", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("[[/some/folder/index]]"); ast = processor.runSync(ast); @@ -630,7 +482,7 @@ describe("remark-wiki-link", () => { describe("other", () => { test("parses a wiki link to some index page in a folder with no matching permalink", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("[[/some/folder/index]]"); ast = processor.runSync(ast); @@ -672,7 +524,7 @@ describe("remark-wiki-link", () => { }); test("parses a wiki link to home index page with no matching permalink", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("[[/index]]"); ast = processor.runSync(ast); @@ -712,7 +564,7 @@ describe("remark-wiki-link", () => { describe("transclusions", () => { test("replaces a transclusion with a regular wiki link", () => { - const processor = createMarkdownProcessorWithWikiLinkPlugin(); + const processor = unified().use(markdown).use(wikiLinkPlugin); let ast = processor.parse("![[Some Page]]"); ast = processor.runSync(ast); From 1663b09a860726ed39bebba34fe634e48a46afcb Mon Sep 17 00:00:00 2001 From: "leonardo.farias" Date: Wed, 1 Nov 2023 21:50:52 -0300 Subject: [PATCH 4/4] Adjusts in the regex used to replace spaces with dashes in the header of wiki links and adjusted the unit tests --- packages/remark-wiki-link/src/lib/fromMarkdown.ts | 2 +- packages/remark-wiki-link/src/lib/html.ts | 2 +- .../test/micromarkExtensionWikiLink.spec.ts | 5 ++--- packages/remark-wiki-link/test/remarkWikiLink.spec.ts | 6 +++--- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/remark-wiki-link/src/lib/fromMarkdown.ts b/packages/remark-wiki-link/src/lib/fromMarkdown.ts index d9e9a0efd..5b5f7b9c8 100644 --- a/packages/remark-wiki-link/src/lib/fromMarkdown.ts +++ b/packages/remark-wiki-link/src/lib/fromMarkdown.ts @@ -116,7 +116,7 @@ function fromMarkdown(opts: FromMarkdownOptions = {}) { // remove leading # if the target is a heading on the same page const displayName = alias || target.replace(/^#/, ""); - const headingId = heading.replace(/\s+/, "-").toLowerCase(); + const headingId = heading.replace(/\s+/g, "-").toLowerCase(); let classNames = wikiLinkClassName; if (!matchingPermalink) { classNames += " " + newClassName; diff --git a/packages/remark-wiki-link/src/lib/html.ts b/packages/remark-wiki-link/src/lib/html.ts index 021970a8e..1058652f6 100644 --- a/packages/remark-wiki-link/src/lib/html.ts +++ b/packages/remark-wiki-link/src/lib/html.ts @@ -99,7 +99,7 @@ function html(opts: HtmlOptions = {}) { // remove leading # if the target is a heading on the same page const displayName = alias || target.replace(/^#/, ""); // replace spaces with dashes and lowercase headings - const headingId = heading.replace(/\s+/, "-").toLowerCase(); + const headingId = heading.replace(/\s+/g, "-").toLowerCase(); let classNames = wikiLinkClassName; if (!matchingPermalink) { classNames += " " + newClassName; diff --git a/packages/remark-wiki-link/test/micromarkExtensionWikiLink.spec.ts b/packages/remark-wiki-link/test/micromarkExtensionWikiLink.spec.ts index 165717e76..663b885c1 100644 --- a/packages/remark-wiki-link/test/micromarkExtensionWikiLink.spec.ts +++ b/packages/remark-wiki-link/test/micromarkExtensionWikiLink.spec.ts @@ -324,12 +324,11 @@ describe("micromark-extension-wiki-link", () => { describe("Links with special characters", () => { test("parses a link with special characters and symbols", () => { - const serialized = micromark("[[li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\]]", "ascii", { + const serialized = micromark("[[li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#LI NK-W(i)th-àcèô íã_a(n)d_uNdErlinE!:ª%@'*º$ °~./\\]]", "ascii", { extensions: [syntax()], htmlExtensions: [html() as any], }); - const prefixExpected = '

li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#LI NK-W(i)th-àcèô íã_a(n)d_uNdErlinE!:ª%@'*º$ °~./\\

`); }); }) }); diff --git a/packages/remark-wiki-link/test/remarkWikiLink.spec.ts b/packages/remark-wiki-link/test/remarkWikiLink.spec.ts index e8f72a886..276038fc6 100644 --- a/packages/remark-wiki-link/test/remarkWikiLink.spec.ts +++ b/packages/remark-wiki-link/test/remarkWikiLink.spec.ts @@ -365,7 +365,7 @@ describe("remark-wiki-link", () => { test("parses a link with special characters and symbols", () => { const processor = unified().use(markdown).use(wikiLinkPlugin); - let ast = processor.parse("[[li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#li-nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\]]"); + let ast = processor.parse("[[li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#li-nk-w(i)th-àcèô íã_a(n)D_UNDERLINE!:ª%@'*º$ °~./\\]]"); ast = processor.runSync(ast); expect(select("wikiLink", ast)).not.toEqual(null); @@ -378,10 +378,10 @@ describe("remark-wiki-link", () => { "internal new" ); expect((node.data?.hProperties as any).href).toEqual( - "li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#li-nk-w(i)th-àcèô-íã_a(n)d_underline!:ª%@'*º$ °~./\\" + "li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#li-nk-w(i)th-àcèô-íã_a(n)d_underline!:ª%@'*º$-°~./\\" ); expect((node.data?.hChildren as any)[0].value).toEqual( - "li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#li-nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\" + "li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#li-nk-w(i)th-àcèô íã_a(n)D_UNDERLINE!:ª%@'*º$ °~./\\" ); }) });