Skip to content

Commit

Permalink
fix: Fix page title computation
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed Nov 5, 2024
1 parent 303b688 commit f9135ce
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/pages/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export type Meta = TitleMeta &

export function generateStaticMarkup(
props: DocInnerProps<DocPageData>,
tocDir: string,
tocName = 'toc',
tocPath: string,
title: string,
): string {
const {style, script, metadata, ...restYamlConfigMeta} = (props.data.meta as Meta) || {};
const resources = getResources({style, script});
Expand All @@ -42,7 +42,7 @@ export function generateStaticMarkup(
<base href="${base}" />
${getMetadata(metadata, restYamlConfigMeta)}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${props.data.title}</title>
<title>${title}</title>
<style type="text/css">
body {
height: 100vh;
Expand All @@ -62,7 +62,7 @@ export function generateStaticMarkup(
window.STATIC_CONTENT = ${staticContent}
window.__DATA__ = ${JSON.stringify(props)};
</script>
<script src="${join(tocDir, tocName + '.js')}" type="application/javascript"></script>
<script src="${tocPath + '.js'}" type="application/javascript"></script>
${manifest.app.js
.map((url: string) => join(BUNDLE_FOLDER, url))
.map(
Expand Down
42 changes: 12 additions & 30 deletions src/resolvers/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const FileTransformer: Record<string, Function> = {
'.md': MdFileTransformer,
};

const getFileMeta = async ({fileExtension, metadata, inputPath}: ResolverOptions) => {
const getFileData = async ({fileExtension, metadata, inputPath}: ResolverOptions) => {
const {input, allowCustomResources} = ArgvService.getConfig();

const resolvedPath: string = resolve(input, inputPath);
Expand Down Expand Up @@ -73,10 +73,9 @@ const getFileMeta = async ({fileExtension, metadata, inputPath}: ResolverOptions

const getFileProps = async (options: ResolverOptions) => {
const {inputPath} = options;
const toc = TocService.getForPath(inputPath)[1];

const {lang: configLang, langs: configLangs, analytics, search} = ArgvService.getConfig();
const meta = await getFileMeta(options);

const data = await getFileData(options);

const tocBaseLang = inputPath.split('/')[0];
const tocLang = configLangs?.includes(tocBaseLang as Lang) && tocBaseLang;
Expand All @@ -86,16 +85,10 @@ const getFileProps = async (options: ResolverOptions) => {

const pathname = inputPath.replace(extname(inputPath), '');

const title = getTitle({
metaTitle: meta.meta.title,
tocTitle: toc?.title as string,
pageTitle: meta.title,
});

return {
data: {
...meta,
title,
...data,
title: data.meta.title || data.title || '',
leading: inputPath.endsWith('.yaml'),
},
router: {
Expand All @@ -118,33 +111,22 @@ export async function resolveMd2HTML(options: ResolverOptions): Promise<DocInner
const {outputPath, inputPath} = options;
const props = await getFileProps(options);

const tocDir = TocService.getForPath(inputPath)[0] as string;
const [tocDir, toc] = TocService.getForPath(inputPath) as [string, YfmToc];

const outputFileContent = generateStaticMarkup(props, tocDir);
const title = getTitle(toc.title as string, props.data.title);
const outputFileContent = generateStaticMarkup(props, join(tocDir, 'toc'), title);
writeFileSync(outputPath, outputFileContent);
logger.info(inputPath, PROCESSING_FINISHED);

return props;
}

interface GetTitleOptions {
tocTitle?: string;
metaTitle?: string;
pageTitle?: string;
}

function getTitle({tocTitle, metaTitle, pageTitle}: GetTitleOptions) {
const resultPageTitle = metaTitle || pageTitle;

if (!resultPageTitle && tocTitle) {
return tocTitle;
}

if (resultPageTitle && !tocTitle) {
return resultPageTitle;
function getTitle(tocTitle: string, dataTitle: string) {
if (dataTitle && tocTitle) {
return `${dataTitle} | ${tocTitle}`;
}

return resultPageTitle && tocTitle ? `${resultPageTitle} | ${tocTitle}` : '';
return tocTitle || dataTitle || '';
}

function getHref(root: string, path: string, href: string) {
Expand Down
19 changes: 15 additions & 4 deletions src/steps/processPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ import {resolveMd2HTML, resolveMd2Md} from '../resolvers';
import {ArgvService, LeadingService, PluginService, SearchService, TocService} from '../services';
import {generateStaticMarkup} from '~/pages/document';
import {generateStaticRedirect} from '~/pages/redirect';
import {joinSinglePageResults, logger, transformToc, transformTocForSinglePage} from '../utils';
import {
getDepth,
joinSinglePageResults,
logger,
transformToc,
transformTocForSinglePage,
} from '../utils';
import {getVCSConnector} from '../vcs-connector';
import {VCSConnector} from '../vcs-connector/connector-models';

Expand Down Expand Up @@ -164,6 +170,8 @@ async function saveSinglePages() {
inputFolderPath,
relativeTocDir,
);

const toc = TocService.getForPath(relativeTocDir + '/toc.yaml')[1] as YfmToc;
const lang = configLang ?? Lang.RU;
const langs = configLangs?.length ? configLangs : [lang];

Expand All @@ -173,10 +181,11 @@ async function saveSinglePages() {
html: singlePageBody,
headings: [],
meta: resources || {},
title: toc.title || '',
},
router: {
pathname: SINGLE_PAGE_FILENAME,
depth: relativeTocDir.split('/').length + 1,
depth: getDepth(relativeTocDir) + 1,
},
lang,
langs,
Expand All @@ -187,8 +196,8 @@ async function saveSinglePages() {
const singlePageDataFn = join(tocDir, SINGLE_PAGE_DATA_FILENAME);
const singlePageContent = generateStaticMarkup(
pageData,
relativeTocDir,
'single-page-toc',
join(relativeTocDir, 'single-page-toc'),
toc.title as string || '',
);

writeFileSync(singlePageFn, singlePageContent);
Expand Down Expand Up @@ -218,6 +227,7 @@ function saveRedirectPage(outputDir: string): void {
function savePageResultForSinglePage(pageProps: DocInnerProps, pathData: PathData): void {
const {pathToFile, outputTocDir} = pathData;

// TODO: allow page-constructor pages?
if (pageProps.data.leading) {
return;
}
Expand All @@ -235,6 +245,7 @@ function savePageResultForSinglePage(pageProps: DocInnerProps, pathData: PathDat
path: pathToFile,
content: pageProps.data.html,
title: pageProps.data.title,
// TODO: handle file resources
});
}

Expand Down

0 comments on commit f9135ce

Please sign in to comment.