Skip to content

Commit

Permalink
fix: Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed Nov 4, 2024
1 parent be6b66d commit 9c47e75
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 211 deletions.
2 changes: 1 addition & 1 deletion scripts/build.cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ Promise.all(builds.map(([entries, outfile]) => {

shell.mkdir('-p', SEARCH_LANGS_OUTPUT);
shell.cp('-f', SEARCH_API, SEARCH_API_OUTPUT);
shell.cp('-f', join(dirname(SEARCH_LANGS), '*'), SEARCH_LANGS_OUTPUT);
shell.cp('-f', join(dirname(SEARCH_LANGS), '*.js'), SEARCH_LANGS_OUTPUT);
});
2 changes: 1 addition & 1 deletion src/pages/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {join} from 'path';
import {BUNDLE_FOLDER, CARRIAGE_RETURN, CUSTOM_STYLE, RTL_LANGS} from '../constants';
import {LeadingPage, Resources, TextItems, VarsMetadata} from '../models';
import {ArgvService, PluginService} from '../services';
import {getDepthPath} from '~/utils';
import {getDepthPath} from '../utils';

import {DocInnerProps, DocPageData, render} from '@diplodoc/client/ssr';
import manifest from '@diplodoc/client/manifest';
Expand Down
4 changes: 2 additions & 2 deletions src/services/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ async function release() {
const dir = outputDir(lang);

mkdirSync(dir, {recursive: true});
writeFileSync(indexLink(lang), index, 'utf8');
writeFileSync(registryLink(lang), registry, 'utf8');
writeFileSync(indexLink(lang), index as string, 'utf8');
writeFileSync(registryLink(lang), registry as string, 'utf8');
writeFileSync(pageLink(lang), generateStaticSearch(lang as Lang), 'utf8');

if (isLocalSearchEnabled() && langs.includes(lang)) {
Expand Down
4 changes: 2 additions & 2 deletions src/steps/processPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ async function saveSinglePages() {
relativeTocDir,
);

const toc = TocService.getForPath(relativeTocDir + '/toc.yaml')[1] as YfmToc;
const toc = TocService.getForPath(join(relativeTocDir, 'toc.yaml'))[1] as YfmToc;
const lang = configLang ?? Lang.RU;
const langs = configLangs?.length ? configLangs : [lang];

Expand All @@ -197,7 +197,7 @@ async function saveSinglePages() {
const singlePageContent = generateStaticMarkup(
pageData,
join(relativeTocDir, 'single-page-toc'),
toc.title as string || '',
(toc.title as string) || '',
);

writeFileSync(singlePageFn, singlePageContent);
Expand Down
40 changes: 28 additions & 12 deletions src/utils/singlePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ function relativeTo(root: string, path: string) {
root = toUrl(root);
path = toUrl(path);

return path.replace(root + '/', '');
if (root && path.startsWith(root + '/')) {
path = path.replace(root + '/', '');
}

return path;
}

function all(root: HTMLElement, selector: string): HTMLElement[] {
return Array.from(root.querySelectorAll(selector));
}

function decreaseHeadingLevels(root: HTMLElement) {
export function decreaseHeadingLevels(root: HTMLElement) {
const headersSelector = 'h1, h2, h3, h4, h5';

root.querySelectorAll(headersSelector).forEach((node) => {
Expand All @@ -43,7 +47,7 @@ function decreaseHeadingLevels(root: HTMLElement) {
});
}

function tryFixFirstPageHeader(root: HTMLElement) {
export function tryFixFirstPageHeader(root: HTMLElement) {
const firstPageHeader = root.querySelector(HEADERS_SELECTOR);
if (!firstPageHeader || firstPageHeader.rawTagName === 'h1') {
return;
Expand All @@ -52,19 +56,20 @@ function tryFixFirstPageHeader(root: HTMLElement) {
firstPageHeader.rawTagName = 'h1';
}

function replaceLinks(rootEl: HTMLElement, options: PreprocessSinglePageOptions) {
export function replaceLinks(root: HTMLElement, options: PreprocessSinglePageOptions) {
const {path, tocDir} = options;

for (const node of all(rootEl, 'a:not(.yfm-anchor):not([target="_blank"])')) {
for (const node of all(root, 'a:not(.yfm-anchor):not([target="_blank"])')) {
const href = node.getAttribute('href') || '';
const linkFullPath = join(dirname(path), href);

// TODO: This is wrong check
// TODO: isLinkOutOfToc is wrong check
// we need to to something like TocService.getForPath
// and then compare with local toc path.
const isLinkOutOfToc = !linkFullPath.startsWith(tocDir);
const isLinkOutOfRoot = linkFullPath.startsWith('../');

if (isLinkOutOfToc) {
if (isLinkOutOfToc || isLinkOutOfRoot) {
return;
}

Expand All @@ -80,12 +85,12 @@ function prepareAnchorAttrs(node: HTMLElement, pathname: string, page: string) {
}

if (name === 'id') {
node.setAttribute(name, `#${page}_${value}`);
node.setAttribute(name, `${page}_${value}`);
}
}
}

function addPagePrefixToAnchors(root: HTMLElement, options: PreprocessSinglePageOptions) {
export function addPagePrefixToAnchors(root: HTMLElement, options: PreprocessSinglePageOptions) {
const {path, tocDir} = options;

const url = getSinglePageUrl(tocDir, path);
Expand All @@ -111,18 +116,29 @@ function addPagePrefixToAnchors(root: HTMLElement, options: PreprocessSinglePage
}
}

function addMainTitle(root: HTMLElement, options: PreprocessSinglePageOptions) {
export function addMainTitle(root: HTMLElement, options: PreprocessSinglePageOptions) {
if (options.title) {
root.insertAdjacentHTML('afterbegin', `<h1>${options.title}</h1>`);
}
}

function getAnchorId(tocDir: string, path: string) {
return relativeTo(tocDir, toUrl(dropExt(path))).replace(/[#/]/g, '_');
const [pathname, hash] = path.split('#');
const url = toUrl(dropExt(pathname)) + (hash ? '#' + hash : '');

// TODO: encodeURIComponent will be best option
return relativeTo(tocDir, url.replace(/\.\.\/|[/#]/g, '_'));
}

export function getSinglePageUrl(tocDir: string, path: string) {
return toUrl(tocDir) + '/single-page.html#' + getAnchorId(tocDir, path);
const prefix = toUrl(tocDir) || '.';
const suffix = getAnchorId(tocDir, path);

if (prefix === '.') {
return '#' + suffix;
}

return prefix + '/single-page.html#' + suffix;
}

export function joinSinglePageResults(
Expand Down
10 changes: 0 additions & 10 deletions tests/e2e/__snapshots__/include-toc.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ exports[`Include toc Nested toc inclusions with mixed including modes 4`] = `
items:
- name: Article1
href: article1.md
base: product1
deepBase: 1
"
`;

Expand Down Expand Up @@ -121,8 +119,6 @@ items:
items:
- name: Article1
href: overlay3/article1.md
base: product2
deepBase: 1
"
`;

Expand Down Expand Up @@ -162,8 +158,6 @@ exports[`Include toc Toc is included in link mode 5`] = `
items:
- name: A1
href: folder1/folder2/a1.md
base: .
deepBase: 0
"
`;

Expand Down Expand Up @@ -291,8 +285,6 @@ exports[`Include toc Toc is included inline, not as a new section 12`] = `
href: fileC.md
- name: NameX
href: fileX.md
base: .
deepBase: 0
"
`;

Expand Down Expand Up @@ -328,7 +320,5 @@ href: index.yaml
items:
- name: A1
href: a1.md
base: .
deepBase: 0
"
`;
61 changes: 27 additions & 34 deletions tests/e2e/__snapshots__/load-custom-resources.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ exports[`Allow load custom resources md2html single page with custom resources 1
"index.html",
"page.html",
"project/config.html",
"single-page-toc.js",
"single-page.html",
"single-page.json",
"toc.js"
Expand Down Expand Up @@ -99,9 +100,9 @@ exports[`Allow load custom resources md2html single page with custom resources 2
</div>
<script type="application/javascript">
window.STATIC_CONTENT = false
window.__DATA__ = {"data":{"data":{"title":"Documentation","description":"","meta":{"title":"Documentation","noIndex":true,"metadata":[],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"links":[{"title":"Getting started with Documentation","description":"This guide will show you the basics of working with Documentation","href":"page.html"}]},"meta":{"title":"Documentation","noIndex":true,"metadata":[],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"title":"Documentation | Documentation","leading":true},"router":{"pathname":"index.html","depth":1},"lang":"ru","langs":["ru"]};
window.__DATA__ = {"data":{"data":{"title":"Documentation","description":"","meta":{"title":"Documentation","noIndex":true,"metadata":[],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"links":[{"title":"Getting started with Documentation","description":"This guide will show you the basics of working with Documentation","href":"page.html"}]},"meta":{"title":"Documentation","noIndex":true,"metadata":[],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"title":"Documentation","leading":true},"router":{"pathname":"index","depth":1},"lang":"ru","langs":["ru"]};
</script>
<script src="./toc.js"
<script src="toc.js"
type="application/javascript"
>
</script>
Expand Down Expand Up @@ -164,11 +165,11 @@ exports[`Allow load custom resources md2html single page with custom resources 3
</div>
<script type="application/javascript">
window.STATIC_CONTENT = false
window.__DATA__ = {"data":{"meta":{"metadata":[{"name":"yfm","content":"builder"}],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"assets":[],"headings":[],"title":"Documentation","includes":[],"html":"
window.__DATA__ = {"data":{"meta":{"metadata":[{"name":"yfm","content":"builder"}],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"assets":[],"headings":[],"title":"","includes":[],"html":"
<p>Lorem
</p>/n","leading":false},"router":{"pathname":"page.html","depth":1},"lang":"ru","langs":["ru"]};
</p>/n","leading":false},"router":{"pathname":"page","depth":1},"lang":"ru","langs":["ru"]};
</script>
<script src="./toc.js"
<script src="toc.js"
type="application/javascript"
>
</script>
Expand Down Expand Up @@ -228,11 +229,11 @@ exports[`Allow load custom resources md2html single page with custom resources 4
</div>
<script type="application/javascript">
window.STATIC_CONTENT = false
window.__DATA__ = {"data":{"meta":{"metadata":[],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"assets":[],"headings":[],"title":"Documentation","includes":[],"html":"
window.__DATA__ = {"data":{"meta":{"metadata":[],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"assets":[],"headings":[],"title":"","includes":[],"html":"
<p>Lorem
</p>/n","leading":false},"router":{"pathname":"project/config.html","depth":2},"lang":"ru","langs":["ru"]};
</p>/n","leading":false},"router":{"pathname":"project/config","depth":2},"lang":"ru","langs":["ru"]};
</script>
<script src="./toc.js"
<script src="toc.js"
type="application/javascript"
>
</script>
Expand All @@ -252,19 +253,21 @@ exports[`Allow load custom resources md2html single page with custom resources 4
</html>
`;
exports[`Allow load custom resources md2html single page with custom resources 5`] = `
exports[`Allow load custom resources md2html single page with custom resources 5`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"#index","items":[{"name":"Documentation","href":"#page","id":"Documentation-RANDOM"},{"name":"Config","href":"#project_config","id":"Config-RANDOM"}]};"`;
exports[`Allow load custom resources md2html single page with custom resources 6`] = `
<!DOCTYPE html>
<html lang="ru"
dir="ltr"
>
<head>
<meta charset="utf-8">
<base href="./">
<base href="../">
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
>
<title>
undefined
Documentation
</title>
<style type="text/css">
body {
Expand Down Expand Up @@ -293,21 +296,13 @@ exports[`Allow load custom resources md2html single page with custom resources 5
<script type="application/javascript">
window.STATIC_CONTENT = false
window.__DATA__ = {"data":{"leading":false,"html":"
<h2 data-original-article=\\"/page.html\\">Documentation
<a class=\\"yfm-anchor\\" aria-hidden=\\"true\\" href=\\"#_page\\" id=\\"_page\\">
</a>
</h2>
<p>Lorem
</p>/n
<hr class=\\"yfm-page__delimeter\\">
<h2 data-original-article=\\"/project/config.html\\">Documentation
<a class=\\"yfm-anchor\\" aria-hidden=\\"true\\" href=\\"#_project_config\\" id=\\"_project_config\\">
</a>
</h2>
<p>Lorem
</p>/n","headings":[],"meta":{"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"toc":{"title":"Documentation","href":"index.yaml","items":[{"name":"Documentation","href":"#_page","id":"Documentation-RANDOM"},{"name":"Config","href":"#_project_config","id":"Config-RANDOM"}],"base":".","deepBase":0,"singlePage":true}},"router":{"pathname":"single-page.html"},"lang":"ru","langs":["ru"]};
</p>/n","headings":[],"meta":{"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"title":"Documentation"},"router":{"pathname":"single-page.html","depth":2},"lang":"ru","langs":["ru"]};
</script>
<script src="./toc.js"
<script src="single-page-toc.js"
type="application/javascript"
>
</script>
Expand All @@ -327,9 +322,9 @@ exports[`Allow load custom resources md2html single page with custom resources 5
</html>
`;
exports[`Allow load custom resources md2html single page with custom resources 6`] = `"{"data":{"leading":false,"html":"<h2 data-original-article=\\"/page.html\\">Documentation<a class=\\"yfm-anchor\\" aria-hidden=\\"true\\" href=\\"#_page\\" id=\\"_page\\"></a></h2><p>Lorem</p>/n<hr class=\\"yfm-page__delimeter\\"><h2 data-original-article=\\"/project/config.html\\">Documentation<a class=\\"yfm-anchor\\" aria-hidden=\\"true\\" href=\\"#_project_config\\" id=\\"_project_config\\"></a></h2><p>Lorem</p>/n","headings":[],"meta":{"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"toc":{"title":"Documentation","href":"index.yaml","items":[{"name":"Documentation","href":"#_page","id":"Documentation-RANDOM"},{"name":"Config","href":"#_project_config","id":"Config-RANDOM"}],"base":".","deepBase":0,"singlePage":true}},"router":{"pathname":"single-page.html"},"lang":"ru","langs":["ru"]}"`;
exports[`Allow load custom resources md2html single page with custom resources 7`] = `"{"data":{"leading":false,"html":"<p>Lorem</p>/n<hr class=\\"yfm-page__delimeter\\"><p>Lorem</p>/n","headings":[],"meta":{"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"title":"Documentation"},"router":{"pathname":"single-page.html","depth":2},"lang":"ru","langs":["ru"]}"`;
exports[`Allow load custom resources md2html single page with custom resources 7`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"Documentation-RANDOM"},{"name":"Config","href":"project/config.html","id":"Config-RANDOM"}],"base":".","deepBase":0};"`;
exports[`Allow load custom resources md2html single page with custom resources 8`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"Documentation-RANDOM"},{"name":"Config","href":"project/config.html","id":"Config-RANDOM"}]};"`;
exports[`Allow load custom resources md2html with custom resources 1`] = `
"[
Expand Down Expand Up @@ -428,9 +423,9 @@ exports[`Allow load custom resources md2html with custom resources 2`] = `
</div>
<script type="application/javascript">
window.STATIC_CONTENT = false
window.__DATA__ = {"data":{"data":{"title":"Documentation","description":"","meta":{"title":"Documentation","noIndex":true,"metadata":[],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"links":[{"title":"Getting started with Documentation","description":"This guide will show you the basics of working with Documentation","href":"page.html"}]},"meta":{"title":"Documentation","noIndex":true,"metadata":[],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"title":"Documentation | Documentation","leading":true},"router":{"pathname":"index.html","depth":1},"lang":"ru","langs":["ru"]};
window.__DATA__ = {"data":{"data":{"title":"Documentation","description":"","meta":{"title":"Documentation","noIndex":true,"metadata":[],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"links":[{"title":"Getting started with Documentation","description":"This guide will show you the basics of working with Documentation","href":"page.html"}]},"meta":{"title":"Documentation","noIndex":true,"metadata":[],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"title":"Documentation","leading":true},"router":{"pathname":"index","depth":1},"lang":"ru","langs":["ru"]};
</script>
<script src="./toc.js"
<script src="toc.js"
type="application/javascript"
>
</script>
Expand Down Expand Up @@ -493,11 +488,11 @@ exports[`Allow load custom resources md2html with custom resources 3`] = `
</div>
<script type="application/javascript">
window.STATIC_CONTENT = false
window.__DATA__ = {"data":{"meta":{"metadata":[{"name":"yfm","content":"builder"}],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"assets":[],"headings":[],"title":"Documentation","includes":[],"html":"
window.__DATA__ = {"data":{"meta":{"metadata":[{"name":"yfm","content":"builder"}],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"assets":[],"headings":[],"title":"","includes":[],"html":"
<p>Lorem
</p>/n","leading":false},"router":{"pathname":"page.html","depth":1},"lang":"ru","langs":["ru"]};
</p>/n","leading":false},"router":{"pathname":"page","depth":1},"lang":"ru","langs":["ru"]};
</script>
<script src="./toc.js"
<script src="toc.js"
type="application/javascript"
>
</script>
Expand Down Expand Up @@ -557,11 +552,11 @@ exports[`Allow load custom resources md2html with custom resources 4`] = `
</div>
<script type="application/javascript">
window.STATIC_CONTENT = false
window.__DATA__ = {"data":{"meta":{"metadata":[],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"assets":[],"headings":[],"title":"Documentation","includes":[],"html":"
window.__DATA__ = {"data":{"meta":{"metadata":[],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"assets":[],"headings":[],"title":"","includes":[],"html":"
<p>Lorem
</p>/n","leading":false},"router":{"pathname":"project/config.html","depth":2},"lang":"ru","langs":["ru"]};
</p>/n","leading":false},"router":{"pathname":"project/config","depth":2},"lang":"ru","langs":["ru"]};
</script>
<script src="./toc.js"
<script src="toc.js"
type="application/javascript"
>
</script>
Expand All @@ -581,7 +576,7 @@ exports[`Allow load custom resources md2html with custom resources 4`] = `
</html>
`;
exports[`Allow load custom resources md2html with custom resources 5`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"Documentation-RANDOM"},{"name":"Config","href":"project/config.html","id":"Config-RANDOM"}],"base":".","deepBase":0};"`;
exports[`Allow load custom resources md2html with custom resources 5`] = `"window.__DATA__.data.toc = {"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"Documentation-RANDOM"},{"name":"Config","href":"project/config.html","id":"Config-RANDOM"}]};"`;
exports[`Allow load custom resources md2md with custom resources 1`] = `
"[
Expand Down Expand Up @@ -654,7 +649,5 @@ items:
href: page.md
- name: Config
href: project/config.md
base: .
deepBase: 0
"
`;
Loading

0 comments on commit 9c47e75

Please sign in to comment.