Skip to content

Commit

Permalink
fix(code): Add ability to disable SEO optimization for titles
Browse files Browse the repository at this point in the history
  • Loading branch information
obenjiro authored and 3y3 committed Nov 8, 2024
1 parent c808d20 commit 4f3f098
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
14 changes: 4 additions & 10 deletions src/transform/plugins/anchors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,14 @@ const removeCustomIds = (token: Token) => {
interface Options {
extractTitle?: boolean;
supportGithubAnchors?: boolean;
disableHeadingsHiddenContent?: boolean;
disableCommonAnchors?: boolean;
transformLink: (v: string) => string;
getPublicPath?: (options: Options, v?: string) => string;
}

const index: MarkdownItPluginCb<Options> = (md, options) => {
const {
extractTitle,
path,
log,
supportGithubAnchors,
getPublicPath,
disableHeadingsHiddenContent,
} = options;
const {extractTitle, path, log, supportGithubAnchors, getPublicPath, disableCommonAnchors} =
options;

const plugin = (state: StateCore) => {
/* Do not use the plugin if it is included in the file */
Expand Down Expand Up @@ -151,7 +145,7 @@ const index: MarkdownItPluginCb<Options> = (md, options) => {
const anchorTitle = removeCustomId(title).replace(/`/g, '');
allAnchorIds.forEach((customId) => {
const setId = id !== customId;
if (!disableHeadingsHiddenContent) {
if (!disableCommonAnchors) {
const linkTokens = createLinkTokens(
state,
customId,
Expand Down
10 changes: 5 additions & 5 deletions test/anchors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,30 +145,30 @@ describe('Anchors', () => {
});
});

describe('with disableHeadingsHiddenContent', () => {
it('should not add anchor links when disableHeadingsHiddenContent is true', () => {
describe('with disableCommonAnchors', () => {
it('should not add anchor links when disableCommonAnchors is true', () => {
const {
result: {html},
} = transform('## Test heading', {
plugins: [includes, anchors],
path: mocksPath,
root: dirname(mocksPath),
getPublicPath,
disableHeadingsHiddenContent: true,
disableCommonAnchors: true,
});

expect(html).toEqual('<h2 id="test-heading">Test heading</h2>\n');
});

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

expect(html).toEqual('<h2 id="custom-id">Test heading</h2>\n');
Expand Down

0 comments on commit 4f3f098

Please sign in to comment.