From 9c47e75fde1a63d2683568aa5accd9d97a282e57 Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Mon, 4 Nov 2024 19:17:29 +0300 Subject: [PATCH] fix: Fix tests --- scripts/build.cli.js | 2 +- src/pages/document.ts | 2 +- src/services/search.ts | 4 +- src/steps/processPages.ts | 4 +- src/utils/singlePage.ts | 40 ++++++--- .../__snapshots__/include-toc.test.ts.snap | 10 --- .../load-custom-resources.spec.ts.snap | 61 ++++++-------- tests/e2e/__snapshots__/metadata.spec.ts.snap | 20 ++--- tests/e2e/__snapshots__/rtl.spec.ts.snap | 84 +++++++++---------- .../__snapshots__/singlePage.test.ts.snap | 58 ++++--------- tests/units/services/singlePage.test.ts | 60 ++----------- 11 files changed, 134 insertions(+), 211 deletions(-) diff --git a/scripts/build.cli.js b/scripts/build.cli.js index 10ec50bf..6c700b16 100644 --- a/scripts/build.cli.js +++ b/scripts/build.cli.js @@ -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); }); diff --git a/src/pages/document.ts b/src/pages/document.ts index 73bdc0d3..2a359239 100644 --- a/src/pages/document.ts +++ b/src/pages/document.ts @@ -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'; diff --git a/src/services/search.ts b/src/services/search.ts index 5c2edf44..cafd028c 100644 --- a/src/services/search.ts +++ b/src/services/search.ts @@ -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)) { diff --git a/src/steps/processPages.ts b/src/steps/processPages.ts index 4c6a554e..501d35fb 100644 --- a/src/steps/processPages.ts +++ b/src/steps/processPages.ts @@ -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]; @@ -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); diff --git a/src/utils/singlePage.ts b/src/utils/singlePage.ts index eeff9dd1..343bcf5b 100644 --- a/src/utils/singlePage.ts +++ b/src/utils/singlePage.ts @@ -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) => { @@ -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; @@ -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; } @@ -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); @@ -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', `