Skip to content

Commit

Permalink
fix: Generate toc item ids on build
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed Jan 9, 2025
1 parent b60f241 commit 45cab0a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 10 deletions.
51 changes: 47 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"tar-stream": "^3.1.4",
"ts-dedent": "^2.2.0",
"typescript": "^5.4.5",
"uuid": "^11.0.4",
"vite-tsconfig-paths": "^4.2.3",
"vitest": "^2.1.8",
"vitest-when": "^0.5.0",
Expand Down
1 change: 1 addition & 0 deletions src/commands/build/core/toc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type Toc = {
};

export type TocItem = NamedTocItem & {hidden?: boolean} & {
id: string;
items?: TocItem[];
};

Expand Down
3 changes: 3 additions & 0 deletions src/commands/build/features/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import type {Build, Toc, TocItem} from '../..';

import {basename, dirname, extname, join} from 'node:path';
import {isExternalHref, own} from '~/utils';
import {v4 as uuid} from 'uuid';

export class Html {
apply(program: Build) {
program.hooks.BeforeRun.for('html').tap('Html', async (run) => {
run.toc.hooks.Resolved.tapPromise('Html', async (toc, path) => {
const copy = JSON.parse(JSON.stringify(toc)) as Toc;
await run.toc.walkItems([copy], (item: Toc | TocItem) => {
item.id = uuid();

if (own<string>(item, 'href') && !isExternalHref(item.href)) {
const fileExtension: string = extname(item.href);
const filename: string = basename(item.href, fileExtension) + '.html';
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/__snapshots__/load-custom-resources.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ exports[`Allow load custom resources md2html single page with custom resources:
exports[`Allow load custom resources md2html single page with custom resources: single-page-toc.js 1`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"#index","items":[{"name":"Documentation","href":"#page"},{"name":"Config","href":"#project_config"}]};"`;
exports[`Allow load custom resources md2html single page with custom resources: toc.js 1`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html"},{"name":"Config","href":"project/config.html"}]};"`;
exports[`Allow load custom resources md2html single page with custom resources: toc.js 1`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"UUID"},{"name":"Config","href":"project/config.html","id":"UUID"}],"id":"UUID"};"`;
exports[`Allow load custom resources md2html with custom resources: .yfm 1`] = `
"resources:
Expand Down Expand Up @@ -596,7 +596,7 @@ exports[`Allow load custom resources md2html with custom resources: project/conf
</html>
`;
exports[`Allow load custom resources md2html with custom resources: toc.js 1`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html"},{"name":"Config","href":"project/config.html"}]};"`;
exports[`Allow load custom resources md2html with custom resources: toc.js 1`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"UUID"},{"name":"Config","href":"project/config.html","id":"UUID"}],"id":"UUID"};"`;
exports[`Allow load custom resources md2md with custom resources: .yfm 1`] = `
"resources:
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/__snapshots__/metadata.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ exports[`Allow load custom resources md2html with metadata: project/config.html
</html>
`;
exports[`Allow load custom resources md2html with metadata: toc.js 1`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html"},{"name":"Config","href":"project/config.html"}]};"`;
exports[`Allow load custom resources md2html with metadata: toc.js 1`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"UUID"},{"name":"Config","href":"project/config.html","id":"UUID"}],"id":"UUID"};"`;
exports[`Allow load custom resources md2md with metadata: filelist 1`] = `
"[
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/__snapshots__/rtl.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
</html>
`;
exports[`Generate html document with correct lang and dir attributes. Load correct bundles. documentation with only one rtl lang: toc.js 1`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html"}]};"`;
exports[`Generate html document with correct lang and dir attributes. Load correct bundles. documentation with only one rtl lang: toc.js 1`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"UUID"}],"id":"UUID"};"`;
exports[`Generate html document with correct lang and dir attributes. Load correct bundles. documentation with rtl and ltr langs: .yfm 1`] = `"langs: ['ar', 'en']"`;
Expand Down Expand Up @@ -279,7 +279,7 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
</html>
`;
exports[`Generate html document with correct lang and dir attributes. Load correct bundles. documentation with rtl and ltr langs: ar/toc.js 1`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"ar/index.html","items":[{"name":"Documentation","href":"ar/page.html"}]};"`;
exports[`Generate html document with correct lang and dir attributes. Load correct bundles. documentation with rtl and ltr langs: ar/toc.js 1`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"ar/index.html","items":[{"name":"Documentation","href":"ar/page.html","id":"UUID"}],"id":"UUID"};"`;
exports[`Generate html document with correct lang and dir attributes. Load correct bundles. documentation with rtl and ltr langs: en/index.html 1`] = `
<!DOCTYPE html>
Expand Down Expand Up @@ -402,7 +402,7 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
</html>
`;
exports[`Generate html document with correct lang and dir attributes. Load correct bundles. documentation with rtl and ltr langs: en/toc.js 1`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"en/index.html","items":[{"name":"Documentation","href":"en/page.html"}]};"`;
exports[`Generate html document with correct lang and dir attributes. Load correct bundles. documentation with rtl and ltr langs: en/toc.js 1`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"en/index.html","items":[{"name":"Documentation","href":"en/page.html","id":"UUID"}],"id":"UUID"};"`;
exports[`Generate html document with correct lang and dir attributes. Load correct bundles. documentation with rtl and ltr langs: filelist 1`] = `
"[
Expand Down
1 change: 1 addition & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const assets = require('@diplodoc/client/manifest');
export function platformless(text: string) {
return text
.replace(/\r\n/g, '\n')
.replace(/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/g, 'UUID')
.replace(/(\\(?![\/"'])){1,2}/g, '/');
}

Expand Down

0 comments on commit 45cab0a

Please sign in to comment.