Skip to content

Commit

Permalink
feat: add disableHtmlExt config feature
Browse files Browse the repository at this point in the history
  • Loading branch information
makamekm committed Apr 26, 2024
1 parent 1382c8a commit 480ecc3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
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 @@ -189,5 +192,6 @@ function MdFileTransformer(content: string, transformOptions: FileTransformOptio
assetsPublicPath: getAssetsPublicPath(filePath),
getVarsPerFile: getVarsPerRelativeFile,
extractTitle: true,
toLinkExtention: options.disableHtmlExt ? '' : null,
});
}
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

0 comments on commit 480ecc3

Please sign in to comment.