Skip to content

Commit

Permalink
Let callbacks take its second arg, an environment
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Jan 30, 2025
1 parent 562e2a9 commit ea28872
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 10 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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/*
Expand All @@ -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
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"fedify",
"fediverse",
"hashtagged",
"hongminhee",
"markdownit"
]
}
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------
Expand Down
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"exclude": [
".dnt-import-map.json",
".github/",
"npm/"
],
"lock": false,
Expand Down
37 changes: 37 additions & 0 deletions src/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ Deno.test("hashtag()", () => {
</blockquote>
<p><a href="https://example.com/">This should be ignored: #FooBar</a></p>
<p><a href="">This also should be ignored: #FooBar</a></p>
`,
);

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,
`\
<ul>
<li><a href="https://example.com/tags/FooBar"><span class="hash">#</span><span class="tag">FooBar</span></a></li>
<li><a href="https://example.com/tags/Baz_Qux"><span class="hash">#</span><span class="tag">Baz_Qux</span></a></li>
<li>#테스트</li>
<li>#Quux</li>
</ul>
`,
);
});
16 changes: 10 additions & 6 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;
// deno-lint-ignore no-explicit-any
linkAttributes?: (handle: string, env: any) => Record<string, string>;

/**
* 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;
}

/**
Expand Down Expand Up @@ -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];
Expand All @@ -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];
Expand Down

0 comments on commit ea28872

Please sign in to comment.