From f90218b3b4489914c7009e66d0a48d70f567b270 Mon Sep 17 00:00:00 2001 From: Max Edell Date: Tue, 18 Jul 2023 03:49:25 -0700 Subject: [PATCH] fix: ignore urns, hrefs, code blocks for rewrite-icons fixes #348 --- src/steps/rewrite-icons.js | 8 ++++++-- test/fixtures/content/icons-ignored.html | 9 +++++++++ test/fixtures/content/icons-ignored.md | 11 +++++++++++ test/fixtures/content/icons.html | 6 ++++-- test/fixtures/content/icons.md | 2 ++ test/rendering.test.js | 4 ++++ 6 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/content/icons-ignored.html create mode 100644 test/fixtures/content/icons-ignored.md diff --git a/src/steps/rewrite-icons.js b/src/steps/rewrite-icons.js index df16842a..d0220451 100644 --- a/src/steps/rewrite-icons.js +++ b/src/steps/rewrite-icons.js @@ -11,9 +11,9 @@ */ /* eslint-disable no-param-reassign */ import { h } from 'hastscript'; -import { CONTINUE, visit } from 'unist-util-visit'; +import { CONTINUE, SKIP, visit } from 'unist-util-visit'; -const REGEXP_ICON = /:(#?[a-z_-]+[a-z\d]*):/gi; +const REGEXP_ICON = /(? icon element: @@ -44,9 +44,13 @@ function createIcon(value) { export default function rewrite({ content }) { const { hast } = content; visit(hast, (node, idx, parent) => { + if (node.tagName === 'code') { + return SKIP; + } if (node.type !== 'text') { return CONTINUE; } + const text = node.value; let lastIdx = 0; for (const match of text.matchAll(REGEXP_ICON)) { diff --git a/test/fixtures/content/icons-ignored.html b/test/fixtures/content/icons-ignored.html new file mode 100644 index 00000000..47f247b3 --- /dev/null +++ b/test/fixtures/content/icons-ignored.html @@ -0,0 +1,9 @@ +
+
+

Icons

+

:button:

+
:rocket:
+

https://example.test/:urn:

+

urn:aaid:sc:VA6C2:ac6066f3-fd1d-4e00-bed3-fa3aa6d981d8

+
+
\ No newline at end of file diff --git a/test/fixtures/content/icons-ignored.md b/test/fixtures/content/icons-ignored.md new file mode 100644 index 00000000..30301a79 --- /dev/null +++ b/test/fixtures/content/icons-ignored.md @@ -0,0 +1,11 @@ +# Icons + +`:button:` + +``` +:rocket: +``` + +[https://example.test/:urn:](https://example.test/:urn:) + +urn:aaid:sc:VA6C2:ac6066f3-fd1d-4e00-bed3-fa3aa6d981d8 diff --git a/test/fixtures/content/icons.html b/test/fixtures/content/icons.html index c1e93873..9ff77789 100644 --- a/test/fixtures/content/icons.html +++ b/test/fixtures/content/icons.html @@ -1,7 +1,9 @@
-

Icons

+
+

Icons

Hello

Hello banner.

Hello mark.

+

Teamblasting off again.

-
+ \ No newline at end of file diff --git a/test/fixtures/content/icons.md b/test/fixtures/content/icons.md index 310a8bfc..b84468f4 100644 --- a/test/fixtures/content/icons.md +++ b/test/fixtures/content/icons.md @@ -5,3 +5,5 @@ Hello :button: Hello :red: banner. Hello :#check: mark. + +Team:rocket:blasting off again. diff --git a/test/rendering.test.js b/test/rendering.test.js index e2082a17..5c17bfdd 100644 --- a/test/rendering.test.js +++ b/test/rendering.test.js @@ -129,6 +129,10 @@ describe('Rendering', () => { it('renders icons.md correctly', async () => { await testRender('icons', 'main'); }); + + it('renders icons-ignored.md correctly', async () => { + await testRender('icons-ignored', 'main'); + }); }); describe('Headings', () => {