-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathArticle.njs
More file actions
184 lines (170 loc) · 6 KB
/
Article.njs
File metadata and controls
184 lines (170 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { readFileSync, existsSync } from 'fs';
import Translatable from './Translatable';
import YAML from 'yaml';
import Arrow from '../icons/Arrow';
import './Article.scss';
import prismjs from 'prismjs';
import { Remarkable } from 'remarkable';
import meta from 'remarkable-meta';
class Article extends Translatable {
title = '';
html = '';
prepare({ router }) {
if (router.path === '/renderable-components' || router.path === '/functional-components') {
router.path = '/stateless-components'
}
if (router.path === '/njs-file-extension') {
router.path = '/jsx-elements'
}
}
static async getArticleByKey({ locale, key }) {
await import('prismjs/components/prism-jsx.min');
let path = `i18n/${locale}/articles/${key}.md`;
if (!existsSync(path)) {
path = `i18n/${locale}/articles/404.md`;
}
const text = readFileSync(path, 'utf-8');
const md = new Remarkable({
highlight: (code) => Prism.highlight(code, prismjs.languages.jsx, 'javascript')
});
md.use(meta);
md.use((md) => {
const originalRender = md.renderer.rules.link_open;
md.renderer.rules.link_open = function () {
let result = originalRender.apply(null, arguments);
const regexp = /href="([^"]*)"/;
const href = regexp.exec(result)[1];
if (!href.startsWith('/')) {
result = result.replace('>', ' target="_blank" rel="noopener">');
}
return result;
};
});
md.use((md) => {
md.renderer.rules.heading_open = function (tokens, i) {
const { content } = tokens[i + 1];
const { hLevel } = tokens[i];
const id = content.toLowerCase().split(/[^a-z]/).join('-');
return `<h${hLevel} id="${id}"><a href="#${id}">`;
}
md.renderer.rules.heading_close = function (tokens, i) {
const { hLevel } = tokens[i];
return `</a></h${hLevel}>`;
}
});
return {
html: md.render(text),
...md.meta
}
}
static async getArticlesList({ locale }) {
const file = readFileSync(`i18n/${locale}/components/Documentation.yml`, 'utf-8');
return YAML.parse(file);
}
async initiate({ page, params }) {
super.initiate({ page })
const article = await this.getArticleByKey({ key: params.slug, locale: page.locale });
Object.assign(this, article);
const { topics } = await this.getArticlesList({ locale: page.locale });
this.topics = topics
}
launch({ project, page }) {
page.title = `${this.title} - ${project.name}`;
page.description = this.description;
if (this.status) {
page.status = 404;
}
}
async hydrate({ router, page }) {
const examples = [
"/how-to-deploy-to-vercel",
"/how-to-deploy-to-github-pages",
"/how-to-deploy-to-heroku",
"/how-to-use-mongodb-with-nullstack",
"/how-to-use-google-analytics-with-nullstack",
"/how-to-use-facebook-pixel-with-nullstack",
]
if (examples.includes(router.url)) {
page.status = 301
router.url = `/examples${router.url}`
}
}
renderLink({ title, href, router }) {
const active = router.url === href;
return (
<a
href={href}
class={[active ? 'text-pink-500 dark:text-pink-500' : 'text-gray-500 dark:text-gray-300', 'hover:text-pink-600 dark:hover:text-pink-400 block w-full']}
onclick={{ expanded: true }}
>
{title}
</a>
)
}
renderTopic({ title, links }) {
return (
<div class="mb-3">
<h5> {title} </h5>
<nav>
{links.map((link) => <Link {...link} />)}
</nav>
</div>
)
}
renderNextArticle({ router }) {
let nextLink, nextTopic
for (let topicIndex = 0; topicIndex < this.topics.length; topicIndex++) {
const topic = this.topics[topicIndex]
for (let linkIndex = 0; linkIndex < topic.links.length; linkIndex++) {
const link = topic.links[linkIndex]
if (link.href === router.path) {
console.log({topicIndex}, topic.links.length)
if (linkIndex < topic.links.length - 1) {
nextLink = topic.links[linkIndex + 1]
nextTopic = topic
} else if (topicIndex < this.topics.length - 1) {
nextTopic = this.topics[topicIndex + 1]
nextLink = nextTopic.links[0]
}
}
}
}
return (
<div class="prose dark:prose-dark max-w-none my-12 border border-pink-600 p-6">
<h2> <a href="#next-step"> {this.i18n.next} </a> </h2>
{nextLink &&
<span class="my-2 block">
➡️ {this.i18n.learn} <a href={nextLink.href} class="text-white hover:text-pink-600">{nextTopic.title}: {nextLink.title}</a>
</span>
}
<span>
❓ {this.i18n.lead} <a href="https://discord.gg/eDZfKz264v" target="_blank">{this.i18n.cta}</a>
</span>
</div>
)
}
render() {
if (!this.html) return false
return (
<section class="max-w-screen-xl mx-auto px-4 flex flex-wrap sm:flex-nowrap py-12 sm:py-24">
<button
onclick={{ expanded: !this.expanded }}
class={['transition delay-300 fixed bottom-10 right-10 bg-pink-600 text-white shadow-xl rounded-full py-2 px-4 z-50 md:hidden ring-0 transform', this.expanded && 'rotate-180 transform']}>
<Arrow size={20} />
</button>
<aside class={[
'w-full md:w-3/12 fixed top-0 left-0 md:relative z-40 md:z-auto bg-white dark:bg-gray-900 h-screen md:h-auto p-4 overflow-y-auto md:p-0 md:pr-4 transform pb-24 sm:pb-0 transition sm:transition-none delay-500 delay-0',
this.expanded ? 'translate-y-0' : 'translate-y-full md:translate-y-0'
]}>
{this.topics?.map((topic) => <Topic {...topic} />)}
</aside>
<article class="w-full md:w-9/12 pb-24">
<h1 class="text-pink-600 text-4xl font-light block mb-8"> {this.title} </h1>
<div html={this.html} class="prose dark:prose-dark max-w-none" />
<NextArticle />
</article>
</section>
)
}
}
export default Article;