Skip to content
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

fix: Fix missed toc data for static-content #882

Merged
merged 2 commits into from
Nov 13, 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
12 changes: 7 additions & 5 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@
]
},
"dependencies": {
"@diplodoc/client": "^3.1.6",
"@diplodoc/client": "^3.1.8",
"@diplodoc/translation": "^1.4.3",
"csp-header": "^5.2.1",
"katex": "^0.16.9",
"shelljs": "0.8.5",
"threads": "1.7.0",
Expand Down Expand Up @@ -90,6 +89,7 @@
"axios": "^1.6.7",
"chalk": "^4.1.2",
"commander": "^12.0.0",
"csp-header": "^5.2.1",
"esbuild": "^0.23.1",
"glob": "^8.0.3",
"html-escaper": "^3.0.3",
Expand Down
18 changes: 14 additions & 4 deletions src/pages/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
DEFAULT_CSP_SETTINGS,
RTL_LANGS,
} from '../constants';
import {LeadingPage, Resources, TextItems, VarsMetadata} from '../models';
import {LeadingPage, Resources, TextItems, VarsMetadata, YfmToc} from '../models';
import {ArgvService, PluginService} from '../services';
import {getDepthPath} from '../utils';

Expand All @@ -26,16 +26,26 @@ export type Meta = TitleMeta &
metadata: VarsMetadata;
};

type TocInfo = {
content: YfmToc;
path: string;
};

export function generateStaticMarkup(
props: DocInnerProps<DocPageData>,
tocPath: string,
toc: TocInfo,
title: string,
): string {
/* @todo replace rest operator with proper unpacking */
const {style, script, csp, metadata, ...restYamlConfigMeta} = (props.data.meta as Meta) || {};
const resources = getResources({style, script});

const {staticContent} = ArgvService.getConfig();
if (staticContent) {
// TODO: there shoul be two different types YfmToc and YfmProcessedToc
// @ts-ignore
props.data.toc = toc.content;
}

const depth = props.router.depth;
const html = staticContent ? render(props) : '';
Expand Down Expand Up @@ -71,7 +81,7 @@ export function generateStaticMarkup(
window.STATIC_CONTENT = ${staticContent}
window.__DATA__ = ${JSON.stringify(props)};
</script>
<script src="${tocPath + '.js'}" type="application/javascript"></script>
<script src="${toc.path + '.js'}" type="application/javascript"></script>
${manifest.app.js
.map((url: string) => join(BUNDLE_FOLDER, url))
.map(
Expand Down Expand Up @@ -113,7 +123,7 @@ function getMetadata(metadata: VarsMetadata | undefined, restMeta: LeadingPage['
}

function generateCSP(csp?: Record<string, string[]>[]) {
if (!csp) {
if (!csp || !csp.length) {
return '';
}

Expand Down
6 changes: 5 additions & 1 deletion src/resolvers/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ export async function resolveMd2HTML(options: ResolverOptions): Promise<DocInner
const [tocDir, toc] = TocService.getForPath(inputPath) as [string, YfmToc];

const title = getTitle(toc.title as string, props.data.title);
const outputFileContent = generateStaticMarkup(props, join(tocDir, 'toc'), title);
const tocInfo = {
content: toc,
path: join(tocDir, 'toc'),
};
const outputFileContent = generateStaticMarkup(props, tocInfo, title);
writeFileSync(outputPath, outputFileContent);
logger.info(inputPath, PROCESSING_FINISHED);

Expand Down
2 changes: 1 addition & 1 deletion src/steps/processPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ async function saveSinglePages() {
const singlePageDataFn = join(tocDir, SINGLE_PAGE_DATA_FILENAME);
const singlePageContent = generateStaticMarkup(
pageData,
join(relativeTocDir, 'single-page-toc'),
{path: join(relativeTocDir, 'single-page-toc'), content: toc},
(toc.title as string) || '',
);

Expand Down
18 changes: 0 additions & 18 deletions tests/e2e/__snapshots__/load-custom-resources.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ exports[`Allow load custom resources md2html single page with custom resources:
<meta name="noIndex"
content="true"
>
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down Expand Up @@ -136,9 +133,6 @@ exports[`Allow load custom resources md2html single page with custom resources:
<meta name="yfm"
content="builder"
>
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down Expand Up @@ -203,9 +197,6 @@ exports[`Allow load custom resources md2html single page with custom resources:
<head>
<meta charset="utf-8">
<base href="../">
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down Expand Up @@ -400,9 +391,6 @@ exports[`Allow load custom resources md2html with custom resources: index.html 1
<meta name="noIndex"
content="true"
>
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down Expand Up @@ -468,9 +456,6 @@ exports[`Allow load custom resources md2html with custom resources: page.html 1`
<meta name="yfm"
content="builder"
>
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down Expand Up @@ -535,9 +520,6 @@ exports[`Allow load custom resources md2html with custom resources: project/conf
<head>
<meta charset="utf-8">
<base href="../">
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down
9 changes: 0 additions & 9 deletions tests/e2e/__snapshots__/metadata.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ exports[`Allow load custom resources md2html with metadata: index.html 1`] = `
<meta name="noIndex"
content="true"
>
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down Expand Up @@ -136,9 +133,6 @@ exports[`Allow load custom resources md2html with metadata: page.html 1`] = `
<meta name="yfm-config"
content="config test"
>
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down Expand Up @@ -205,9 +199,6 @@ exports[`Allow load custom resources md2html with metadata: project/config.html
<meta name="yfm-config"
content="config test"
>
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down
18 changes: 0 additions & 18 deletions tests/e2e/__snapshots__/rtl.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
<head>
<meta charset="utf-8">
<base href="./">
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down Expand Up @@ -114,9 +111,6 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
<head>
<meta charset="utf-8">
<base href="./">
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down Expand Up @@ -176,9 +170,6 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
<head>
<meta charset="utf-8">
<base href="../">
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down Expand Up @@ -234,9 +225,6 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
<head>
<meta charset="utf-8">
<base href="../">
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down Expand Up @@ -302,9 +290,6 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
<meta name="noIndex"
content="true"
>
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down Expand Up @@ -363,9 +348,6 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
<meta name="yfm"
content="builder"
>
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self';"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
Expand Down
Loading