Skip to content

fix: auto header config heading generate func #2474

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

Merged
merged 2 commits into from
Jul 28, 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
26 changes: 17 additions & 9 deletions src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,16 @@ export class Compiler {
this.linkTarget === '_blank' ? config.externalLinkRel || 'noopener' : '';
this.contentBase = router.getBasePath();

const renderer = this._initRenderer();
this.heading = renderer.heading;
this.renderer = this._initRenderer();
let compile;
const mdConf = config.markdown || {};

if (isFn(mdConf)) {
compile = mdConf(marked, renderer);
compile = mdConf(marked, this.renderer);
} else {
marked.setOptions(
Object.assign(mdConf, {
renderer: Object.assign(renderer, mdConf.renderer),
renderer: Object.assign(this.renderer, mdConf.renderer),
}),
);
compile = marked;
Expand Down Expand Up @@ -318,12 +317,21 @@ export class Compiler {
return treeTpl(tree);
}

/**
* Compile the text to generate HTML heading element based on the level
* @param {*} text Text content, for now it is only from the _sidebar.md file
* @param {*} level Type of heading (h<level> tag), for now it is always 1
* @returns
*/
header(text, level) {
return this.heading(text, level);
}

article(text) {
return this.compile(text);
const tokenHeading = {
type: 'heading',
raw: text,
depth: level,
text: text,
tokens: [{ type: 'text', raw: text, text: text }],
};
return this.renderer.heading(tokenHeading);
}

/**
Expand Down
86 changes: 86 additions & 0 deletions test/e2e/sidebar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,89 @@ test.describe('Sidebar Tests', () => {
expect(page.url()).toMatch(/\/test%3Efoo$/);
});
});

test.describe('Configuration: autoHeader', () => {
test('autoHeader=false', async ({ page }) => {
const docsifyInitConfig = {
config: {
loadSidebar: '_sidebar.md',
autoHeader: false,
},
markdown: {
sidebar: `
- [QuickStartAutoHeader](quickstart.md)
`,
},
routes: {
'/quickstart.md': `
the content of quickstart space
## In the main content there is no h1
`,
},
};

await docsifyInit(docsifyInitConfig);

await page.click('a[href="#/quickstart"]');
expect(page.url()).toMatch(/\/quickstart$/);
// not heading
await expect(page.locator('#quickstart')).toBeHidden();
});

test('autoHeader=true', async ({ page }) => {
const docsifyInitConfig = {
config: {
loadSidebar: '_sidebar.md',
autoHeader: true,
},
markdown: {
sidebar: `
- [QuickStartAutoHeader](quickstart.md )
`,
},
routes: {
'/quickstart.md': `
the content of quickstart space
## In the main content there is no h1
`,
},
};

await docsifyInit(docsifyInitConfig);

await page.click('a[href="#/quickstart"]');
expect(page.url()).toMatch(/\/quickstart$/);

// auto generate default heading id
const autoHeader = page.locator('#quickstartautoheader');
expect(await autoHeader.innerText()).toContain('QuickStartAutoHeader');
});

test('autoHeader=true and custom headingId', async ({ page }) => {
const docsifyInitConfig = {
config: {
loadSidebar: '_sidebar.md',
autoHeader: true,
},
markdown: {
sidebar: `
- [QuickStartAutoHeader](quickstart.md ":id=quickstartId")
`,
},
routes: {
'/quickstart.md': `
the content of quickstart space
## In the main content there is no h1
`,
},
};

await docsifyInit(docsifyInitConfig);

await page.click('a[href="#/quickstart"]');
expect(page.url()).toMatch(/\/quickstart$/);
// auto generate custom heading id
const autoHeader = page.locator('#quickstartId');
expect(await autoHeader.innerText()).toContain('QuickStartAutoHeader');
});
});
Loading