Skip to content

Commit

Permalink
chore: Remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
spring-raining committed Aug 25, 2024
1 parent 92764d8 commit 888192a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 145 deletions.
140 changes: 0 additions & 140 deletions src/processor/html.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,107 +243,6 @@ export const defaultTocTransform = {
},
};

export async function generateTocHtml({
entries,
manifestPath,
distDir,
language,
title,
tocTitle,
sectionDepth,
stylesheets = [],
styleOptions = {},
transform = {},
}: {
entries: Pick<ManuscriptEntry, 'target' | 'title'>[];
manifestPath: string;
distDir: string;
language?: string;
title?: string;
tocTitle: string;
sectionDepth: number;
stylesheets?: string[];
styleOptions?: Parameters<typeof getTocHtmlStyle>[0];
transform?: Partial<typeof defaultTocTransform>;
}): Promise<string> {
const {
transformDocumentList = defaultTocTransform.transformDocumentList,
transformSectionList = defaultTocTransform.transformSectionList,
} = transform;

const structure = await Promise.all(
entries.map(async (entry) => {
const href = encodeURI(upath.relative(distDir, entry.target));
const sections =
sectionDepth >= 1
? await getStructuredSectionFromHtml(entry.target, href)
: [];
return {
title: entry.title || upath.basename(entry.target, '.html'),
href: encodeURI(upath.relative(distDir, entry.target)),
sections,
children: [], // TODO
};
}),
);
const docToc = transformDocumentList(structure)(
structure.map((doc) => {
function renderSectionList(
sections: StructuredDocumentSection[],
): HastElement | HastElement[] {
const nodeList = sections.flatMap((section) => {
if (section.level > sectionDepth) {
return [];
}
return section;
});
if (nodeList.length === 0) {
return [];
}
return transformSectionList(nodeList)(
nodeList.map((node) => ({
children: [renderSectionList(node.children || [])].flat(),
})),
);
}
return {
children: [renderSectionList(doc.sections || [])].flat(),
};
}),
);

const toc = (
<html lang={language}>
<head>
<meta charset="utf-8" />
<title>{title || ''}</title>
{(() => {
const style = getTocHtmlStyle(styleOptions);
return style ? <style>{style}</style> : null;
})()}
<link
href={encodeURI(upath.relative(distDir, manifestPath))}
rel="publication"
type="application/ld+json"
/>
{stylesheets.map((s) => (
<link type="text/css" href={s} rel="stylesheet" />
))}
</head>
<body>
<h1>{title || ''}</h1>
<nav id="toc" role="doc-toc">
<h2>{tocTitle}</h2>
{docToc}
</nav>
</body>
</html>
);
return prettier.format(toHtml(toc, { allowDangerousHtml: true }), {
parser: 'html',
});
}

export function generateDefaultTocHtml({
language,
title,
Expand Down Expand Up @@ -507,45 +406,6 @@ body {
margin: 0;
}
`;
export function generateCoverHtml({
imageSrc,
imageAlt,
language,
title,
stylesheets = [],
styleOptions = {},
}: {
imageSrc: string;
imageAlt: string;
language?: string;
title?: string;
stylesheets?: string[];
styleOptions?: Parameters<typeof getCoverHtmlStyle>[0];
}): string {
const cover = (
<html lang={language}>
<head>
<meta charset="utf-8" />
<title>{title || ''}</title>
{(() => {
const style = getCoverHtmlStyle(styleOptions);
return style ? <style>{style}</style> : null;
})()}
{stylesheets.map((s) => (
<link type="text/css" href={s} rel="stylesheet" />
))}
</head>
<body>
<section role="region" aria-label="Cover">
<img src={imageSrc} alt={imageAlt} role="doc-cover" />
</section>
</body>
</html>
);
return prettier.format(toHtml(cover, { allowDangerousHtml: true }), {
parser: 'html',
});
}

export function generateDefaultCoverHtml({
language,
Expand Down
13 changes: 8 additions & 5 deletions tests/toc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { afterAll, expect, it } from 'vitest';
import { MergedConfig } from '../src/input/config.js';
import { compile, prepareThemeDirectory } from '../src/processor/compile.js';
import {
generateTocHtml,
generateDefaultTocHtml,
getStructuredSectionFromHtml,
processTocHtml,
} from '../src/processor/html.js';
import { removeSync } from '../src/util.js';
import {
Expand All @@ -33,27 +34,29 @@ afterAll(() => {
});

it('generateTocHtml', async () => {
const toc = await generateTocHtml({
let content = generateDefaultTocHtml({
title: 'Book title',
});
content = await processTocHtml(content, {
entries: [
{ target: resolveFixture('toc/manuscript/empty.html'), title: 'Title' },
],
manifestPath: resolveFixture(
'toc/manuscript/.vivliostyle/publication.json',
),
distDir: resolveFixture('toc/manuscript/.vivliostyle'),
title: 'Book title',
tocTitle: 'Table of Contents',
sectionDepth: 0,
});
expect(toc).toBe(
expect(content).toBe(
`<html>
<head>
<meta charset="utf-8" />
<title>Book title</title>
<link
href="publication.json"
rel="publication"
type="application/ld+json"
href="publication.json"
/>
</head>
<body>
Expand Down

0 comments on commit 888192a

Please sign in to comment.