Skip to content

Commit

Permalink
feat: implement base path
Browse files Browse the repository at this point in the history
  • Loading branch information
makamekm authored and 3y3 committed Apr 23, 2024
1 parent 269af89 commit dd0cdf2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export interface ResolveMd2MdOptions {
}

export interface ResolverOptions {
deep: number;
inputPath: string;
filename: string;
fileExtension: string;
Expand Down
6 changes: 3 additions & 3 deletions src/resolvers/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const getFileProps = async (options: ResolverOptions) => {
const props = {
data: {
leading: inputPath.endsWith('.yaml'),
toc: transformToc(toc, pathToDir) || {},
toc: transformToc(toc) || {},
...meta,
},
router: {
Expand All @@ -117,13 +117,13 @@ const getFileProps = async (options: ResolverOptions) => {
}

export async function resolveMd2HTML(options: ResolverOptions): Promise<DocInnerProps> {
const { outputPath, outputBundlePath, inputPath } = options;
const { outputPath, outputBundlePath, inputPath, deep } = options;
const props = await getFileProps(options);

const outputDir = dirname(outputPath);
const relativePathToBundle: string = relative(resolve(outputDir), resolve(outputBundlePath));

const outputFileContent = generateStaticMarkup(props, relativePathToBundle);
const outputFileContent = generateStaticMarkup(props, relativePathToBundle, deep);
writeFileSync(outputPath, outputFileContent);
logger.info(inputPath, PROCESSING_FINISHED);

Expand Down
3 changes: 3 additions & 0 deletions src/steps/processPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,15 @@ async function processingFileToHtml(
): Promise<DocInnerProps> {
const {outputBundlePath, filename, fileExtension, outputPath, pathToFile} = path;

const deep = pathToFile.split('/').length - 2; // exclude lang and count slashes

return resolveMd2HTML({
inputPath: pathToFile,
outputBundlePath,
fileExtension,
outputPath,
filename,
metadata: metaDataOptions,
deep,
});
}
5 changes: 4 additions & 1 deletion src/utils/markup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type Meta = TitleMeta &
export function generateStaticMarkup(
props: DocInnerProps<DocPageData>,
pathToBundle: string,
deep: number = 0,
): string {
const {style, script, metadata, ...restYamlConfigMeta} = (props.data.meta as Meta) || {};
const {title: tocTitle} = props.data.toc;
Expand All @@ -45,12 +46,14 @@ export function generateStaticMarkup(

const html = staticContent ? render(props) : '';
const isRTL = RTL_LANGS.includes(props.lang);
const deepPath = deep ? Array(deep).fill('../').join('') : './';

return `
<!DOCTYPE html>
<html lang="${props.lang}" dir="${isRTL ? 'rtl' : 'ltr'}">
<head>
<meta charset="utf-8">
<base href="${deepPath}" target="_blank" />
${getMetadata(metadata, restYamlConfigMeta)}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${title}</title>
Expand All @@ -60,7 +63,7 @@ export function generateStaticMarkup(
}
</style>
${manifest.css
.filter((file) => isRTL === file.includes('.rtl.css'))
.filter((file: string) => isRTL === file.includes('.rtl.css'))
.map(dst(pathToBundle))
.map((src: string) => `<link type="text/css" rel="stylesheet" href="${src}" />`)
.join('\n')}
Expand Down
14 changes: 3 additions & 11 deletions src/utils/toc.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {basename, dirname, extname, format, join, relative} from 'path';
import {basename, dirname, extname, join} from 'path';

import {YfmToc} from '../models';
import {filterFiles} from '../services/utils';
import {isExternalHref} from './url';
import {getSinglePageAnchorId} from './singlePage';

export function transformToc(toc: YfmToc | null, pathToFileDirectory: string): YfmToc | null {
export function transformToc(toc: YfmToc | null): YfmToc | null {
if (!toc) {
return null;
}
Expand All @@ -23,7 +23,6 @@ export function transformToc(toc: YfmToc | null, pathToFileDirectory: string): Y
);
}

const baseTocPath: string = localToc.base || '';
const navigationItemQueue = [localToc];

while (navigationItemQueue.length) {
Expand All @@ -40,17 +39,10 @@ export function transformToc(toc: YfmToc | null, pathToFileDirectory: string): Y
}

if (href && !isExternalHref(href)) {
/* Path to directory with toc.yaml */
const pathToIndexDirectory: string = relative(pathToFileDirectory, baseTocPath);

const fileExtension: string = extname(href);
const filename: string = basename(href, fileExtension);
const transformedFilename: string = format({
name: filename,
ext: '.html',
});

navigationItem.href = join(pathToIndexDirectory, dirname(href), transformedFilename);
navigationItem.href = join(dirname(href), filename);
}
}

Expand Down

0 comments on commit dd0cdf2

Please sign in to comment.