Skip to content

Commit

Permalink
feat(anchors): remove unsupported chars from heading ids
Browse files Browse the repository at this point in the history
Passed a stricter regexp to slugify, thats remove all unsupported symbols from ids

Read more about the supported characters here: https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
  • Loading branch information
d3m1d0v authored and 3y3 committed Nov 15, 2024
1 parent 4b136b3 commit 34c3f1b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
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
4 changes: 2 additions & 2 deletions test/__snapshots__/anchors.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ exports[`Anchors should include content by anchor in sharped path file 1`] = `
`;

exports[`Anchors should remove quotes from id 1`] = `
<h2 id="«heading»-with-quotes">
<a href="link.html#«heading»-with-quotes"
<h2 id="heading-with-quotes">
<a href="link.html#heading-with-quotes"
class="yfm-anchor"
aria-hidden="true"
>
Expand Down

0 comments on commit 34c3f1b

Please sign in to comment.