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

feat: add disableHtmlExt config feature #749

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface YfmConfig {
connector?: VCSConnectorConfig;
lang?: Lang;
langs?: Lang[];
disableHtmlExt: boolean;
lintDisabled: boolean;
buildDisabled: boolean;
lintConfig: LintConfig;
Expand Down
12 changes: 8 additions & 4 deletions src/resolvers/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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: {
Expand All @@ -119,6 +119,9 @@ export async function resolveMd2HTML(options: ResolverOptions): Promise<DocInner
}

function YamlFileTransformer(content: string, transformOptions: FileTransformOptions): Object {
const {disableHtmlExt} = ArgvService.getConfig();
const ext = disableHtmlExt ? '' : '.html';

let data: LeadingPage | null = null;

try {
Expand All @@ -136,7 +139,7 @@ function YamlFileTransformer(content: string, transformOptions: FileTransformOpt
if (Object.prototype.hasOwnProperty.call(data, 'blocks')) {
data = modifyValuesByKeys(data, LINK_KEYS, (link) => {
if (isString(link) && getLinksWithContentExtersion(link)) {
return link.replace(/.(md|yaml)$/gmu, '.html');
return link.replace(/.(md|yaml)$/gmu, ext);
}
});

Expand All @@ -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) {
Expand Down Expand Up @@ -190,5 +193,6 @@ function MdFileTransformer(content: string, transformOptions: FileTransformOptio
rootPublicPath: getAssetsRootPath(filePath),
getVarsPerFile: getVarsPerRelativeFile,
extractTitle: true,
toLinkExtention: options.disableHtmlExt ? '' : null,
});
}
8 changes: 6 additions & 2 deletions src/steps/processPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import shell from 'shelljs';
import {
Lang,
PAGE_PROCESS_CONCURRENCY,
REGEXP_EXT_HTML,
ResourceType,
SINGLE_PAGE_DATA_FILENAME,
SINGLE_PAGE_FILENAME,
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 7 additions & 5 deletions src/utils/toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down
Loading