Skip to content

Commit 2682b6b

Browse files
obenjiro3y3
authored andcommitted
fix(code): Add ability to disable SEO optimization for titles
1 parent d8ad8a5 commit 2682b6b

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/transform/plugins/anchors/index.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ const removeCustomIds = (token: Token) => {
7676
interface Options {
7777
extractTitle?: boolean;
7878
supportGithubAnchors?: boolean;
79+
disableSEOFixForTitles?: boolean;
7980
transformLink: (v: string) => string;
8081
getPublicPath?: (options: Options, v?: string) => string;
8182
}
8283

8384
const index: MarkdownItPluginCb<Options> = (md, options) => {
84-
const {extractTitle, path, log, supportGithubAnchors, getPublicPath} = options;
85+
const {extractTitle, path, log, supportGithubAnchors, getPublicPath, disableSEOFixForTitles} =
86+
options;
8587

8688
const plugin = (state: StateCore) => {
8789
/* Do not use the plugin if it is included in the file */
@@ -143,9 +145,16 @@ const index: MarkdownItPluginCb<Options> = (md, options) => {
143145
const anchorTitle = removeCustomId(title).replace(/`/g, '');
144146
allAnchorIds.forEach((customId) => {
145147
const setId = id !== customId;
146-
const linkTokens = createLinkTokens(state, customId, anchorTitle, setId, href);
147-
148-
inlineToken.children?.unshift(...linkTokens);
148+
if (!disableSEOFixForTitles) {
149+
const linkTokens = createLinkTokens(
150+
state,
151+
customId,
152+
anchorTitle,
153+
setId,
154+
href,
155+
);
156+
inlineToken.children?.unshift(...linkTokens);
157+
}
149158

150159
if (supportGithubAnchors) {
151160
const ghLinkTokens = createLinkTokens(state, ghId, anchorTitle, true, href);

test/anchors.test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,34 @@ describe('Anchors', () => {
144144
expect(result[1]).toBe('Test');
145145
});
146146
});
147+
148+
describe('with disableSEOFixForTitles', () => {
149+
it('should not add anchor links when disableSEOFixForTitles is true', () => {
150+
const {
151+
result: {html},
152+
} = transform('## Test heading', {
153+
plugins: [includes, anchors],
154+
path: mocksPath,
155+
root: dirname(mocksPath),
156+
getPublicPath,
157+
disableSEOFixForTitles: true,
158+
});
159+
160+
expect(html).toEqual('<h2 id="test-heading">Test heading</h2>\n');
161+
});
162+
163+
it('should not add anchor links for custom anchors when disableSEOFixForTitles is true', () => {
164+
const {
165+
result: {html},
166+
} = transform('## Test heading {#custom-id}', {
167+
plugins: [includes, anchors],
168+
path: mocksPath,
169+
root: dirname(mocksPath),
170+
getPublicPath,
171+
disableSEOFixForTitles: true,
172+
});
173+
174+
expect(html).toEqual('<h2 id="custom-id">Test heading</h2>\n');
175+
});
176+
});
147177
});

0 commit comments

Comments
 (0)