Skip to content

Commit 19c275f

Browse files
committed
tmp(pdf): add kotlin tour
1 parent 76a8909 commit 19c275f

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

scripts/dist/pdf/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const TOC_PATH = join(DOCS_PATH, 'HelpTOC.json');
1717

1818
const TOC = JSON.parse(await readFile(TOC_PATH, { encoding: 'utf-8' }));
1919

20+
21+
2022
const nodes = new Map(
2123
(await convertToFlatUrls(TOC))
2224
.map(id => new URL(id, 'https://kotlinlang.org/docs/'))

scripts/dist/pdf/lib.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
import { FileType } from '../lib/files/type.js';
2+
13
export type Result = {
24
id: string
35
html: string
46
}
57

8+
export function isHiddenProved(type: FileType, relativePath: string) {
9+
return type === 'Hidden' && (
10+
relativePath.startsWith('docs/kotlin-tour-') ||
11+
relativePath === 'docs/multiplatform.html'
12+
);
13+
}
14+
615
export async function convertToFlatUrls({ topLevelIds, entities }: {
716
entities: { pages: Record<string, { url?: string, pages: string[] }> },
817
topLevelIds: string[]
@@ -22,6 +31,20 @@ export async function convertToFlatUrls({ topLevelIds, entities }: {
2231
const { url, pages } = page;
2332

2433
if (url) result.push(page.url);
34+
if (url === 'kotlin-tour-welcome.html') {
35+
const tour = [
36+
'kotlin-tour-hello-world',
37+
'kotlin-tour-basic-types',
38+
'kotlin-tour-collections',
39+
'kotlin-tour-control-flow',
40+
'kotlin-tour-functions',
41+
'kotlin-tour-classes',
42+
'kotlin-tour-null-safety'
43+
];
44+
for (const id of tour) {
45+
result.push(`${id}.html`);
46+
}
47+
}
2548

2649
if (pages?.length) {
2750
for (let i = page.pages.length - 1; i >= 0; i--) {

scripts/dist/pdf/task.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { CheerioAPI } from 'cheerio';
33
import { Element } from 'domhandler';
44
import { getType } from '../lib/files/type.js';
55
import { DIST_FOLDER } from '../lib/files/index.js';
6-
import { Result } from './lib.js';
6+
import { isHiddenProved, Result } from './lib.js';
77
import { replaceNode } from '../lib/html.js';
88

99
function sendEvent(...args: Parameters<typeof process.send>) {
@@ -22,7 +22,13 @@ const HOME_HTML_CONTENT = '' +
2222
'</article><div id="disqus_thread"></div ></div></section>';
2323

2424

25-
async function fixSectionHtml($: CheerioAPI, node: Element) {
25+
function buildAnchor(filename: string, hash: string) {
26+
let result = '#' + filename.replace(/\.html$/, '.md');
27+
if (hash) result += '-' + hash.substring(1);
28+
return result;
29+
}
30+
31+
async function fixSectionHtml($: CheerioAPI, node: Element, relativePath: string) {
2632
const $node = $(node);
2733

2834
$node.find('.last-modified, .navigation-links._bottom').remove();
@@ -76,18 +82,23 @@ async function fixSectionHtml($: CheerioAPI, node: Element) {
7682
$node.find('[id]:not(h1)').each(function(_i, node) {
7783
const article = $(node).closest('.article');
7884
const h1Id = article.find('h1[id$=".md"]').attr('id');
79-
if (h1Id && article.length === 1) node.attribs .id = h1Id + '-' + node.attribs.id;
85+
if (h1Id && article.length === 1) node.attribs.id = h1Id + '-' + node.attribs.id;
8086
});
8187
$node.find('a[href]').each(function(_i, node) {
88+
let anchor = '';
89+
8290
const href = node.attribs.href;
8391
const url = new URL(href, 'https://kotlinlang.org/docs/');
84-
if (url.hostname === 'kotlinlang.org' && dirname(url.pathname) === '/docs') {
92+
93+
if (href[0] === '#') anchor = buildAnchor(basename(relativePath), href);
94+
else if (url.hostname === 'kotlinlang.org' && dirname(url.pathname) === '/docs') {
8595
const filename = basename(url.pathname);
86-
if (filename.endsWith('.html')) {
87-
let anchor = '#' + filename.replace(/\.html$/, '.md')
88-
if (url.hash) anchor += '-' + url.hash.slice(1);
89-
node.attribs.href = anchor;
90-
}
96+
if (filename.endsWith('.html')) anchor = buildAnchor(filename, url.hash);
97+
}
98+
99+
if (anchor) {
100+
node.attribs.href = anchor;
101+
node.attribs['data-origin-href'] = href;
91102
}
92103
});
93104

@@ -101,7 +112,10 @@ async function onMessage(relativePath: string) {
101112
let html: string = null;
102113

103114
const path = join(DIST_FOLDER, relativePath);
104-
const [type, $] = await getType(relativePath, path);
115+
let [type, $] = await getType(relativePath, path);
116+
117+
if (isHiddenProved(type, relativePath))
118+
type = 'Page_Documentation';
105119

106120
if (type === 'Page_Documentation') {
107121
const sections = $('section.panel__content');
@@ -111,7 +125,7 @@ async function onMessage(relativePath: string) {
111125
} else if (sections.length > 0) {
112126
html = '';
113127
for (const node of sections) {
114-
html += await fixSectionHtml($, node);
128+
html += await fixSectionHtml($, node, relativePath);
115129
}
116130
}
117131
}

0 commit comments

Comments
 (0)