diff --git a/src/transform/plugins/anchors/index.ts b/src/transform/plugins/anchors/index.ts index 857ed038..b217f76a 100644 --- a/src/transform/plugins/anchors/index.ts +++ b/src/transform/plugins/anchors/index.ts @@ -76,12 +76,14 @@ const removeCustomIds = (token: Token) => { interface Options { extractTitle?: boolean; supportGithubAnchors?: boolean; + disableSEOFixForTitles?: boolean; transformLink: (v: string) => string; getPublicPath?: (options: Options, v?: string) => string; } const index: MarkdownItPluginCb = (md, options) => { - const {extractTitle, path, log, supportGithubAnchors, getPublicPath} = options; + const {extractTitle, path, log, supportGithubAnchors, getPublicPath, disableSEOFixForTitles} = + options; const plugin = (state: StateCore) => { /* Do not use the plugin if it is included in the file */ @@ -143,9 +145,16 @@ const index: MarkdownItPluginCb = (md, options) => { const anchorTitle = removeCustomId(title).replace(/`/g, ''); allAnchorIds.forEach((customId) => { const setId = id !== customId; - const linkTokens = createLinkTokens(state, customId, anchorTitle, setId, href); - - inlineToken.children?.unshift(...linkTokens); + if (!disableSEOFixForTitles) { + const linkTokens = createLinkTokens( + state, + customId, + anchorTitle, + setId, + href, + ); + inlineToken.children?.unshift(...linkTokens); + } if (supportGithubAnchors) { const ghLinkTokens = createLinkTokens(state, ghId, anchorTitle, true, href); diff --git a/test/anchors.test.ts b/test/anchors.test.ts index d5ab238a..b1088b1e 100644 --- a/test/anchors.test.ts +++ b/test/anchors.test.ts @@ -144,4 +144,34 @@ describe('Anchors', () => { expect(result[1]).toBe('Test'); }); }); + + describe('with disableSEOFixForTitles', () => { + it('should not add anchor links when disableSEOFixForTitles is true', () => { + const { + result: {html}, + } = transform('## Test heading', { + plugins: [includes, anchors], + path: mocksPath, + root: dirname(mocksPath), + getPublicPath, + disableSEOFixForTitles: true, + }); + + expect(html).toEqual('

Test heading

\n'); + }); + + it('should not add anchor links for custom anchors when disableSEOFixForTitles is true', () => { + const { + result: {html}, + } = transform('## Test heading {#custom-id}', { + plugins: [includes, anchors], + path: mocksPath, + root: dirname(mocksPath), + getPublicPath, + disableSEOFixForTitles: true, + }); + + expect(html).toEqual('

Test heading

\n'); + }); + }); });