From ea28872d48cf46523bfd4b3e0326b8a569644f3e Mon Sep 17 00:00:00 2001 From: Hong Minhee Date: Thu, 30 Jan 2025 10:06:28 +0900 Subject: [PATCH] Let callbacks take its second arg, an environment --- .github/workflows/main.yaml | 10 ++++++---- .vscode/settings.json | 1 + CHANGES.md | 7 +++++++ deno.json | 1 + src/plugin.test.ts | 37 +++++++++++++++++++++++++++++++++++++ src/plugin.ts | 16 ++++++++++------ 6 files changed, 62 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index cd70a31..01db16e 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -9,9 +9,9 @@ jobs: pull-requests: write steps: - uses: actions/checkout@v4 - - uses: denoland/setup-deno@v1 + - uses: denoland/setup-deno@v2 with: - deno-version: v1.x + deno-version: v2.x - run: deno test --junit-path=.test-report.xml - uses: EnricoMi/publish-unit-test-result-action@v2 if: always() @@ -28,9 +28,9 @@ jobs: id-token: write steps: - uses: actions/checkout@v4 - - uses: denoland/setup-deno@v1 + - uses: denoland/setup-deno@v2 with: - deno-version: v1.x + deno-version: v2.x - uses: actions/setup-node@v4 with: node-version: lts/* @@ -57,3 +57,5 @@ jobs: NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} working-directory: ${{ github.workspace }}/npm/ - run: deno publish --allow-dirty + +# cSpell: ignore denoland npmjs diff --git a/.vscode/settings.json b/.vscode/settings.json index a0c173b..544eaeb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -23,6 +23,7 @@ "fedify", "fediverse", "hashtagged", + "hongminhee", "markdownit" ] } diff --git a/CHANGES.md b/CHANGES.md index 6c8f174..be7e441 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,13 @@ Version 0.3.0 To be released. + - `PluginOptions.link()` became to have the second parameter which takes + a markdown-it environment object. + - `PluginOptions.linkAttributes()` became to have the second parameter which + takes a markdown-it environment object. + - `PluginOptions.label()` became to have the second parameter which takes + a markdown-it environment object. + Version 0.2.0 ------------- diff --git a/deno.json b/deno.json index 53e6ed8..11398b0 100644 --- a/deno.json +++ b/deno.json @@ -11,6 +11,7 @@ }, "exclude": [ ".dnt-import-map.json", + ".github/", "npm/" ], "lock": false, diff --git a/src/plugin.test.ts b/src/plugin.test.ts index 2e375a5..aafc1c3 100644 --- a/src/plugin.test.ts +++ b/src/plugin.test.ts @@ -38,6 +38,43 @@ Deno.test("hashtag()", () => {

This should be ignored: #FooBar

This also should be ignored: #FooBar

+`, + ); + + const md2 = new MarkdownIt() + .use(hashtag, { + // deno-lint-ignore no-explicit-any + link(tag: string, env: any): string | null { + if ( + !env.allowedPrefixes.some((prefix: string) => tag.startsWith(prefix)) + ) { + return null; + } + return new URL( + `/tags/${encodeURIComponent(tag.substring(1))}`, + env.origin, + ).href; + }, + }); + // deno-lint-ignore no-explicit-any + const env2: any = { + origin: "https://example.com", + allowedPrefixes: ["#Foo", "#Baz"], + }; + const html2 = md2.render( + "- #FooBar\n- #Baz_Qux\n- #테스트\n- #Quux", + env2, + ); + assertEquals(env2.hashtags, ["#FooBar", "#Baz_Qux"]); + assertEquals( + html2, + `\ + `, ); }); diff --git a/src/plugin.ts b/src/plugin.ts index 8c6ac18..e9fc575 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -15,18 +15,21 @@ export interface PluginOptions { * A function to render a link href for a hashtag. If it returns `null`, * the hashtag will be rendered as plain text. `#${tag}` by default. */ - link?: (tag: string) => string | null; + // deno-lint-ignore no-explicit-any + link?: (tag: string, env: any) => string | null; /** * A function to render extra attributes for a hashtag link. */ - linkAttributes?: (handle: string) => Record; + // deno-lint-ignore no-explicit-any + linkAttributes?: (handle: string, env: any) => Record; /** * A function to render a label for a hashtag link. {@link spanHashAndTag} * by default. */ - label?: (handle: string) => string; + // deno-lint-ignore no-explicit-any + label?: (handle: string, env: any) => string; } /** @@ -92,7 +95,7 @@ function splitTokens( tokens.push(token); } - const href = options?.link?.(match[0]); + const href = options?.link?.(match[0], state.env); if (href == null && options?.link != null) { const token = new state.Token("text", "", 0); token.content = match[0]; @@ -103,9 +106,10 @@ function splitTokens( } const token = new state.Token("hashtag", "", 0); - token.content = options?.label?.(match[0]) ?? spanHashAndTag(match[0]); + token.content = options?.label?.(match[0], state.env) ?? + spanHashAndTag(match[0]); token.level = level; - const attrs = options?.linkAttributes?.(match[0]) ?? {}; + const attrs = options?.linkAttributes?.(match[0], state.env) ?? {}; attrs.href = href ?? `${match[0]}`; token.attrs = Object.entries(attrs); token.info = match[0];