Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(anchors): remove unsupported chars from heading ids #558

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"markdownlint": "^0.32.1",
"markdownlint-rule-helpers": "0.17.2",
"sanitize-html": "^2.11.0",
"slugify": "1.6.5",
"slugify": "1.6.6",
"svgo": "^3.2.0"
},
"devDependencies": {
Expand Down
5 changes: 2 additions & 3 deletions src/transform/plugins/anchors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import GithubSlugger from 'github-slugger';
import StateCore from 'markdown-it/lib/rules_core/state_core';
import Token from 'markdown-it/lib/token';
import {escapeHtml} from 'markdown-it/lib/common/utils';
import slugify from 'slugify';

import {headingInfo} from '../../utils';
import {MarkdownItPluginCb} from '../typings';

import {CUSTOM_ID_EXCEPTION, CUSTOM_ID_REGEXP} from './constants';

const slugify: (str: string, opts: {}) => string = require('slugify');

function createLinkTokens(
state: StateCore,
id: string,
Expand Down Expand Up @@ -127,7 +126,7 @@ const index: MarkdownItPluginCb<Options> = (md, options) => {
} else {
id = slugify(title, {
lower: true,
remove: /[*+~.()'"!:@`ь?]/g,
remove: /[^\w\s$_\-,;=/]+/g,
});
ghId = slugger.slug(title);
}
Expand Down
38 changes: 38 additions & 0 deletions test/__snapshots__/anchors.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ exports[`Anchors should add anchor with auto naming, using entire heading text 1
</p>
`;

exports[`Anchors should add id for heading with allowed chars in fragment (RFC 3986) 1`] = `
<h2 id="a-c5percentdollarandabc_;xx,=3/bbb">
<a href="link.html#a-c5percentdollarandabc_;xx,=3/bbb"
class="yfm-anchor"
aria-hidden="true"
>
<span class="visually-hidden"
data-no-index="true"
>
A-c~5%!$&amp;(abc)*_+;xx,@=3/':b?b.b'
</span>
</a>
A-c~5%!$&amp;(abc)*_+;xx,@=3/':b?b.b'
</h2>
<p>
Content
</p>
`;

exports[`Anchors should add multiple anchors 1`] = `
<pre>
<code>
Expand Down Expand Up @@ -234,6 +253,25 @@ exports[`Anchors should include content by anchor in sharped path file 1`] = `
</p>
`;

exports[`Anchors should remove quotes from id 1`] = `
<h2 id="heading-with-quotes">
<a href="link.html#heading-with-quotes"
class="yfm-anchor"
aria-hidden="true"
>
<span class="visually-hidden"
data-no-index="true"
>
«Heading» “with” "quotes'
</span>
</a>
«Heading» “with” "quotes'
</h2>
<p>
Content
</p>
`;

exports[`Anchors with extract title should not add an anchor for level 1 heading 1`] = `
<p>
Content
Expand Down
22 changes: 22 additions & 0 deletions test/anchors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ describe('Anchors', () => {
).toMatchSnapshot();
});

it('should add id for heading with allowed chars in fragment (RFC 3986) ', () => {
expect(
// heading contains all allowed chars in fragment (hash) in accordance with RFC 3986
html(dedent`
## A-c~5%!$&(abc)*_+;xx,@=3/':b?b.b'

Content
`),
).toMatchSnapshot();
});

it('should remove quotes from id', () => {
expect(
// heading contains all allowed chars in fragment (hash) in accordance with RFC 3986
html(dedent`
## «Heading» “with” "quotes'

Content
`),
).toMatchSnapshot();
});

describe('with extract title', () => {
const transformWithTitle = (text: string) => {
const {
Expand Down
Loading