Skip to content

Commit

Permalink
fix: ignore urns, hrefs, code blocks for rewrite-icons
Browse files Browse the repository at this point in the history
fixes #348
  • Loading branch information
maxakuru authored and tripodsan committed Jul 18, 2023
1 parent 4ec0c4d commit f90218b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/steps/rewrite-icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = /(?<!(?:https?|urn)[^\s]*):(#?[a-z_-]+[a-z\d]*):/gi;

/**
* Create a <span> icon element:
Expand Down Expand Up @@ -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)) {
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/content/icons-ignored.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<main>
<div>
<h1 id="icons">Icons</h1>
<p><code>:button:</code></p>
<pre><code>:rocket:</code></pre>
<p><a href="https://example.test/:urn:">https://example.test/:urn:</a></p>
<p>urn:aaid:sc:VA6C2:ac6066f3-fd1d-4e00-bed3-fa3aa6d981d8</p>
</div>
</main>
11 changes: 11 additions & 0 deletions test/fixtures/content/icons-ignored.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Icons

`:button:`

```
:rocket:
```

[https://example.test/:urn:](https://example.test/:urn:)

urn:aaid:sc:VA6C2:ac6066f3-fd1d-4e00-bed3-fa3aa6d981d8
6 changes: 4 additions & 2 deletions test/fixtures/content/icons.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<main>
<div><h1 id="icons">Icons</h1>
<div>
<h1 id="icons">Icons</h1>
<p>Hello <span class="icon icon-button"></span></p>
<p>Hello <span class="icon icon-red"></span> banner.</p>
<p>Hello <span class="icon icon-check"></span> mark.</p>
<p>Team<span class="icon icon-rocket"></span>blasting off again.</p>
</div>
</main>
</main>
2 changes: 2 additions & 0 deletions test/fixtures/content/icons.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ Hello :button:
Hello :red: banner.

Hello :#check: mark.

Team:rocket:blasting off again.
4 changes: 4 additions & 0 deletions test/rendering.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit f90218b

Please sign in to comment.