From 2667ab65805aa3aeb2b880638704ce61e5dc81fa Mon Sep 17 00:00:00 2001 From: Maxim Karpov Date: Fri, 26 Apr 2024 14:21:41 +0300 Subject: [PATCH 1/2] feat: add disableHtmlExt config feature --- src/models.ts | 1 + src/resolvers/md2html.ts | 12 ++++++++---- src/utils/toc.ts | 12 +++++++----- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/models.ts b/src/models.ts index 5a8020ad..3cd78acc 100644 --- a/src/models.ts +++ b/src/models.ts @@ -44,6 +44,7 @@ interface YfmConfig { connector?: VCSConnectorConfig; lang?: Lang; langs?: Lang[]; + disableHtmlExt: boolean; lintDisabled: boolean; buildDisabled: boolean; lintConfig: LintConfig; diff --git a/src/resolvers/md2html.ts b/src/resolvers/md2html.ts index 43f1f7cb..cb45da5c 100644 --- a/src/resolvers/md2html.ts +++ b/src/resolvers/md2html.ts @@ -82,7 +82,7 @@ const getFileProps = async (options: ResolverOptions) => { const pathToFileDir: string = pathToDir === tocBase ? '' : pathToDir.replace(`${tocBase}${sep}`, ''); - const {lang: configLang, langs: configLangs} = ArgvService.getConfig(); + const {lang: configLang, langs: configLangs, disableHtmlExt} = ArgvService.getConfig(); const meta = await getFileMeta(options); const tocBaseLang = tocBase?.split('/')[0]; @@ -94,7 +94,7 @@ const getFileProps = async (options: ResolverOptions) => { const props = { data: { leading: inputPath.endsWith('.yaml'), - toc: transformToc(toc) || {}, + toc: transformToc(toc, disableHtmlExt) || {}, ...meta, }, router: { @@ -119,6 +119,9 @@ export async function resolveMd2HTML(options: ResolverOptions): Promise { if (isString(link) && getLinksWithContentExtersion(link)) { - return link.replace(/.(md|yaml)$/gmu, '.html'); + return link.replace(/.(md|yaml)$/gmu, ext); } }); @@ -149,7 +152,7 @@ function YamlFileTransformer(content: string, transformOptions: FileTransformOpt }); } else { const links = data?.links?.map((link) => - link.href ? {...link, href: link.href.replace(/.md$/gmu, '.html')} : link, + link.href ? {...link, href: link.href.replace(/.md$/gmu, ext)} : link, ); if (links) { @@ -190,5 +193,6 @@ function MdFileTransformer(content: string, transformOptions: FileTransformOptio rootPublicPath: getAssetsRootPath(filePath), getVarsPerFile: getVarsPerRelativeFile, extractTitle: true, + toLinkExtention: options.disableHtmlExt ? '' : null, }); } diff --git a/src/utils/toc.ts b/src/utils/toc.ts index 56758b10..2cb41d4f 100644 --- a/src/utils/toc.ts +++ b/src/utils/toc.ts @@ -5,7 +5,7 @@ import {filterFiles} from '../services/utils'; import {isExternalHref} from './url'; import {getSinglePageAnchorId} from './singlePage'; -export function transformToc(toc: YfmToc | null): YfmToc | null { +export function transformToc(toc: YfmToc | null, disableHtmlExt: boolean): YfmToc | null { if (!toc) { return null; } @@ -41,10 +41,12 @@ export function transformToc(toc: YfmToc | null): YfmToc | null { if (href && !isExternalHref(href)) { const fileExtension: string = extname(href); const filename: string = basename(href, fileExtension); - const transformedFilename: string = format({ - name: filename, - ext: '.html', - }); + const transformedFilename: string = disableHtmlExt + ? filename + : format({ + name: filename, + ext: '.html', + }); navigationItem.href = join(dirname(href), transformedFilename); } From fc60ba85d2847a3bdc359030afafbafc7baf67d8 Mon Sep 17 00:00:00 2001 From: Maxim Karpov Date: Fri, 26 Apr 2024 18:14:35 +0300 Subject: [PATCH 2/2] feat: add disableHtmlExt config feature to links and redirect --- src/constants.ts | 2 ++ src/steps/processPages.ts | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 94b594bf..e3da84ce 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -133,6 +133,8 @@ export const REGEXP_INCLUDE_FILE_PATH = /(?<=[(]).+(?=[)])/g; // Regexp result: authorLogin export const REGEXP_AUTHOR = /(?<=author:\s).+(?=\r?\n)/g; +export const REGEXP_EXT_HTML = /(\.html)$/gi; + export const MIN_CHUNK_SIZE = Number(process.env.MIN_CHUNK_SIZE) || 1000; export const WORKERS_COUNT = Number(process.env.WORKERS_COUNT) || os.cpus().length - 1; export const PAGE_PROCESS_CONCURRENCY = Number(process.env.PAGE_PROCESS_CONCURRENCY) || 500; diff --git a/src/steps/processPages.ts b/src/steps/processPages.ts index c7fd4bd9..6b79329e 100644 --- a/src/steps/processPages.ts +++ b/src/steps/processPages.ts @@ -10,6 +10,7 @@ import shell from 'shelljs'; import { Lang, PAGE_PROCESS_CONCURRENCY, + REGEXP_EXT_HTML, ResourceType, SINGLE_PAGE_DATA_FILENAME, SINGLE_PAGE_FILENAME, @@ -189,15 +190,18 @@ async function saveSinglePages() { } function saveRedirectPage(outputDir: string): void { - const {lang, langs} = ArgvService.getConfig(); + const {lang, langs, disableHtmlExt} = ArgvService.getConfig(); const redirectLang = lang || langs?.[0] || Lang.RU; - const redirectLangRelativePath = `./${redirectLang}/index.html`; + let redirectLangRelativePath = `./${redirectLang}/index.html`; const redirectPagePath = join(outputDir, 'index.html'); const redirectLangPath = join(outputDir, redirectLangRelativePath); if (!existsSync(redirectPagePath) && existsSync(redirectLangPath)) { + if (disableHtmlExt) { + redirectLangRelativePath = redirectLangRelativePath.replace(REGEXP_EXT_HTML, ''); + } const content = generateStaticRedirect(redirectLang, redirectLangRelativePath); writeFileSync(redirectPagePath, content); }