Skip to content

Commit

Permalink
fix: auto header config heading generate func (#2474)
Browse files Browse the repository at this point in the history
  • Loading branch information
Koooooo-7 authored Jul 28, 2024
1 parent 49f5c56 commit 4bc5062
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 9 deletions.
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');
});
});

0 comments on commit 4bc5062

Please sign in to comment.