From f5c617e8dbf36695e6ed9b683838af5b983712ff Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Fri, 1 Nov 2024 13:16:26 +0800 Subject: [PATCH] feat: add `tagName` option. #4 --- README.md | 21 +++++++++++++++++++++ src/index.ts | 11 ++++++++--- test/index.test.ts | 10 ++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 465ca6f..9d2bf7d 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,27 @@ The output HTML will be: ``` +## Wrap tag name with container + +```js +let markdown = `> [!CAUTION] \n> Hello World`; +const htmlStr = remark() + .use(remarkParse) + .use(remarkAlert, { tagName: "blockquote" }) + .use(remarkRehype) + .use(rehypeStringify) + .processSync(markdown).toString() +``` + +The output HTML will be: + +```html +
+

CAUTION

+

Hello World

+
+``` + ## Styling You can mimic GitHub's alert style by adding the styles provided in the npm package to your CSS. diff --git a/src/index.ts b/src/index.ts index 83d3208..abf7964 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,12 @@ type Option = { * * Enabling legacyTitle allows modifying the title, but this is not GitHub standard. */ - legacyTitle?: boolean + legacyTitle?: boolean; + /** + * The tag name of the alert container. default is `div`. + * or you can use `blockquote` for semantic HTML. + */ + tagName?: string; } /** @@ -19,7 +24,7 @@ type Option = { * On GitHub, they are displayed with distinctive colors and icons to indicate the significance of the content. * https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts */ -export const remarkAlert: Plugin<[Option?], Root> = ({ legacyTitle = false } = {}) => { +export const remarkAlert: Plugin<[Option?], Root> = ({ legacyTitle = false, tagName = "div" } = {}) => { return (tree) => { visit(tree, "blockquote", (node, index, parent) => { let alertType = ''; @@ -60,7 +65,7 @@ export const remarkAlert: Plugin<[Option?], Root> = ({ legacyTitle = false } = { if (!!alertType) { node.data = { - hName: "div", + hName: tagName, hProperties: { class: `markdown-alert markdown-alert-${alertType}`, dir: 'auto' diff --git a/test/index.test.ts b/test/index.test.ts index 4efa891..e1e7609 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -104,4 +104,14 @@ Urgent info that needs immediate user attention to avoid problems.

`; const htmlStr = remark().use(remarkParse).use(remarkAlert).use(remarkRehype).use(rehypeStringify).processSync(markdown).toString(); expect(htmlStr).toEqual(html); +}); + +it('Alert `tagName` option test case 1', async () => { + const markdown = `> [!CAUTION] \n> Hello World`; + const html = `
+

CAUTION

+

Hello World

+
`; + const htmlStr = remark().use(remarkParse).use(remarkAlert, { tagName: "blockquote" }).use(remarkRehype).use(rehypeStringify).processSync(markdown).toString() + expect(htmlStr).toEqual(html); }); \ No newline at end of file