From b9f4f5c404d5b55531768b6a7f7ae3d5ba827ae7 Mon Sep 17 00:00:00 2001 From: koy Date: Fri, 26 Jul 2024 00:15:34 +0800 Subject: [PATCH 1/2] fix: autoheader config heading generate func --- src/core/render/compiler.js | 26 +++++++---- test/e2e/sidebar.test.js | 93 +++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 9 deletions(-) diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index 443356213..caed15561 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -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; @@ -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 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); } /** diff --git a/test/e2e/sidebar.test.js b/test/e2e/sidebar.test.js index d41fe95cd..622ac3c88 100644 --- a/test/e2e/sidebar.test.js +++ b/test/e2e/sidebar.test.js @@ -69,3 +69,96 @@ 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$/); + // const element = page.locator('#main'); + // expect(await element.innerText()).toContain( + // 'In the main content there is no h1', + // ); + // expect(await element.innerText()).toContain( + // 'the content of quickstart space', + // ); + // 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'); + }); +}); From 00824aa2dc41e5aa99c10d5a5708ea9b53dec1f8 Mon Sep 17 00:00:00 2001 From: koy Date: Fri, 26 Jul 2024 00:53:14 +0800 Subject: [PATCH 2/2] fix: autoheader config heading generate func --- test/e2e/sidebar.test.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/test/e2e/sidebar.test.js b/test/e2e/sidebar.test.js index 622ac3c88..ff4526601 100644 --- a/test/e2e/sidebar.test.js +++ b/test/e2e/sidebar.test.js @@ -94,13 +94,6 @@ test.describe('Configuration: autoHeader', () => { await page.click('a[href="#/quickstart"]'); expect(page.url()).toMatch(/\/quickstart$/); - // const element = page.locator('#main'); - // expect(await element.innerText()).toContain( - // 'In the main content there is no h1', - // ); - // expect(await element.innerText()).toContain( - // 'the content of quickstart space', - // ); // not heading await expect(page.locator('#quickstart')).toBeHidden(); });