|
1 | | -import { readdirSync, readFileSync, writeFileSync } from 'fs'; |
| 1 | +import { readdirSync } from 'fs'; |
2 | 2 | import Nullstack from "nullstack"; |
3 | 3 | import path from 'path'; |
4 | 4 | import Application from "./src/Application"; |
5 | | -import prismjs from 'prismjs'; |
6 | | -import { Remarkable } from 'remarkable'; |
7 | | -import meta from 'remarkable-meta'; |
8 | 5 | import 'prismjs/components/prism-jsx.min'; |
9 | 6 |
|
10 | 7 | const context = Nullstack.start(Application); |
11 | 8 |
|
12 | | -const { worker, project, environment } = context; |
13 | | - |
14 | | -function slugify(string) { |
15 | | - return string.normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/[^a-zA-Z ]/g, "").toLowerCase() |
16 | | -} |
17 | | - |
18 | | -const locales = ['en-US', 'pt-BR'] |
19 | | -context.articles = {} |
20 | | - |
21 | | -for (const locale of locales) { |
22 | | - context.articles[locale] = {} |
23 | | - const articles = readdirSync(path.join(__dirname, `../i18n/${locale}`, 'articles')); |
24 | | - // preload files for workers |
25 | | - if (locale === 'en-US') { |
26 | | - const illustrations = readdirSync(path.join(__dirname, '../public', 'illustrations')); |
27 | | - worker.preload = [ |
28 | | - ...articles.map((article) => '/' + article.replace('.md', '')).filter((article) => article.indexOf('404') === -1), |
29 | | - ...illustrations.map((illustration) => '/illustrations/' + illustration), |
30 | | - '/en-US.json', |
31 | | - '/arrow.webp', |
32 | | - '/stars.webp', |
33 | | - '/footer.webp', |
34 | | - '/contributors', |
35 | | - '/roboto-v20-latin-300.woff2', |
36 | | - '/roboto-v20-latin-500.woff2', |
37 | | - '/crete-round-v9-latin-regular.woff2', |
38 | | - ] |
39 | | - } |
40 | | - const map = {} |
41 | | - for (const article of articles) { |
42 | | - const content = readFileSync(path.join(__dirname, `../i18n/${locale}`, 'articles', article), 'utf-8') |
43 | | - // preload articles markdown |
44 | | - const md = new Remarkable({ |
45 | | - highlight: (code) => Prism.highlight(code, prismjs.languages.jsx, 'javascript') |
46 | | - }); |
47 | | - md.use(meta); |
48 | | - md.use((md) => { |
49 | | - const originalRender = md.renderer.rules.link_open; |
50 | | - md.renderer.rules.link_open = function () { |
51 | | - let result = originalRender.apply(null, arguments); |
52 | | - const regexp = /href="([^"]*)"/; |
53 | | - const href = regexp.exec(result)[1]; |
54 | | - if (!href.startsWith('/')) { |
55 | | - result = result.replace('>', ' target="_blank" rel="noopener">'); |
56 | | - } |
57 | | - return result; |
58 | | - }; |
59 | | - }); |
60 | | - md.use((md) => { |
61 | | - md.renderer.rules.heading_open = function (tokens, i) { |
62 | | - const { content } = tokens[i + 1]; |
63 | | - const { hLevel } = tokens[i]; |
64 | | - const id = content.toLowerCase().split(/[^a-z]/).join('-'); |
65 | | - return `<h${hLevel} id="${id}"><a href="#${id}">`; |
66 | | - } |
67 | | - md.renderer.rules.heading_close = function (tokens, i) { |
68 | | - const { hLevel } = tokens[i]; |
69 | | - return `</a></h${hLevel}>`; |
70 | | - } |
71 | | - }); |
72 | | - context.articles[locale][article] = { |
73 | | - html: md.render(content), |
74 | | - ...md.meta, |
75 | | - } |
76 | | - // generate word map for search |
77 | | - const lines = [] |
78 | | - let shouldSkip = false |
79 | | - for (const line of content.split("\n")) { |
80 | | - if (line.startsWith('```')) { |
81 | | - shouldSkip = !shouldSkip |
82 | | - } else if (!shouldSkip && !(line.includes('[') && line.includes(']'))) { |
83 | | - lines.push(line) |
84 | | - } |
85 | | - } |
86 | | - const words = lines.join(" ").split(" ") |
87 | | - const wordMap = {} |
88 | | - for (const word of words) { |
89 | | - const slug = slugify(word) |
90 | | - if (!slug) continue |
91 | | - if (!wordMap[slug]) { |
92 | | - wordMap[slug] = 1 |
93 | | - } else { |
94 | | - wordMap[slug]++ |
95 | | - } |
96 | | - } |
97 | | - const key = article.replace('.md', '') |
98 | | - map[key] = { |
99 | | - ...md.meta, |
100 | | - href: locale === 'en-US' ? `/${key}` : `/${locale.toLowerCase()}/${key}`, |
101 | | - words: wordMap |
102 | | - } |
103 | | - } |
104 | | - const json = environment.development ? JSON.stringify(map, null, 2) : JSON.stringify(map) |
105 | | - writeFileSync(`public/${locale}.json`, json) |
106 | | -} |
107 | | - |
| 9 | +const { worker, project } = context; |
| 10 | + |
| 11 | +const illustrations = readdirSync(path.join(__dirname, '../public', 'illustrations')); |
| 12 | +const articles = readdirSync(path.join(__dirname, `../i18n/en-US`, 'articles')); |
| 13 | +worker.preload = [ |
| 14 | + ...articles.map((article) => '/' + article.replace('.md', '')).filter((article) => article.indexOf('404') === -1), |
| 15 | + ...illustrations.map((illustration) => '/illustrations/' + illustration), |
| 16 | + '/arrow.webp', |
| 17 | + '/stars.webp', |
| 18 | + '/footer.webp', |
| 19 | + '/contributors', |
| 20 | + '/roboto-v20-latin-300.woff2', |
| 21 | + '/roboto-v20-latin-500.woff2', |
| 22 | + '/crete-round-v9-latin-regular.woff2', |
| 23 | +] |
108 | 24 |
|
109 | 25 | project.name = 'Nullstack'; |
110 | 26 | project.domain = 'nullstack.app'; |
|
0 commit comments