-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·363 lines (298 loc) · 11.3 KB
/
index.js
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import MarkDownIt from 'markdown-it';
import matter from "gray-matter";
import fs from 'fs-extra';
import path from 'path';
import attrs from 'markdown-it-attrs';
import markdownToken from 'markdown-it-modify-token';
import markdownFootnote from 'markdown-it-footnote';
import { URL } from 'url';
// Define the placeholders and directory paths
const MARKDOWNIFY_CONTENT_PLACEHOLDER = '<!--markdownify content-->'
const MARKDOWNIFY_META_PLACEHOLDER = '<!--markdownify meta-->'
const DEFAULT_TEMPLATE_PATH = './index.html'
const DEFAULT_MARKDOWN_DIR = path.resolve('./markdown');
const DEFAULT_OUTPUT_DIR = process.cwd();
const DEFAULT_FEED_PLACEHOLDER = '<!-- markdown items -->'
const DEFAULT_SITEMAP_PLACEHOLDER = '<!-- markdown items -->'
const markdownIt = createMarkdownItInstance();
// Define the tags with meta data name
const META_TAGS_WITH_NAME = [
'owner', 'author', 'application-name', 'generator', 'referrer', 'theme-color',
'copyright', 'medium', 'language', 'description', 'keywords', 'robots', 'viewport'
];
const markdownify = (options = {}) => {
const {
input,
output,
defaults,
contentPlaceholder,
metaPlaceholder,
words_per_minute,
htmlTemplate,
feedTemplate,
feedContentPlaceholder,
sitemapTemplate,
sitemapContentPlaceholder,
doNotRenderFeed,
doNotRenderSitemap,
renderings
} = options;
const config = {
templatePath: htmlTemplate || DEFAULT_TEMPLATE_PATH,
feedTemplate,
sitemapTemplate,
feedContentPlaceholder: feedContentPlaceholder || DEFAULT_FEED_PLACEHOLDER,
sitemapContentPlaceholder: sitemapContentPlaceholder || DEFAULT_SITEMAP_PLACEHOLDER,
doNotRenderFeed,
doNotRenderSitemap,
markdownDir: input || DEFAULT_DIRS.markdown,
outputDir: output || DEFAULT_DIRS.output,
markdownify_content_placeholder: contentPlaceholder || MARKDOWNIFY_CONTENT_PLACEHOLDER,
markdownify_meta_placeholder: metaPlaceholder || MARKDOWNIFY_META_PLACEHOLDER,
words_per_minute,
renderings,
defaults
}
return {
name: 'vite-plugin-markdownify',
// apply: 'all', // Apply this plugin during both development and production
async writeBundle() {
await renderMarkdownFiles(config);
},
transformIndexHtml: {
enforce: 'pre',
transform: (html, { path: url }) => {
if (process.env.NODE_ENV !== 'development') {
return html;
}
const pages = getFiles(config.markdownDir)
.map(file => processFile(file, config))
.filter(file => file)
.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
const page = pages.find(p => p.filename === 'index')
return substituteHtml(html, page, pages, config)
}
}
};
};
// Function to render all markdown files in a directory
async function renderMarkdownFiles(config) {
const template = fs.readFileSync(config.templatePath, 'utf-8'); // Read the HTML template
const pages = getFiles(config.markdownDir)
.map(file => processFile(file, config))
.filter(file => file)
.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
pages
.map(page => ({
...page,
output: substituteHtml(template, page, pages, config)
}))
.map(page => makeFile(`${page.filename}.html`, config.outputDir, page.output))
if(typeof config.renderings === 'object') {
Object.keys(config.renderings).forEach(filepath => {
const construct = config.renderings[filepath]
if(construct && construct.render) {
const items = construct.map ? pages.map(p => construct.map(p, config)).filter(p => p).join(''): pages
makeFile(filepath, config.outputDir, construct.render(items, config))
}
})
}
if(!config.doNotRenderSitemap) {
makeSitemap(pages, config)
}
if(!config.doNotRenderFeed) {
makeFeed(pages, config)
}
}
const formatDate = date => new Date(date).getTime(); // Function to format date
// Function to process a markdown file into HTML
function processFile(file, { markdownDir, defaults, words_per_minute }) {
const fileInfo = matter(fs.readFileSync(file, 'utf-8')); // Read the markdown file
if (fileInfo.data.draft) { // If it is a draft, don't process it
return false;
}
const stats = fs.statSync(file); // Get file statistics
const filename = path.relative(markdownDir, file).replace(/\.md$/, '') // Get filename
const html = markdownIt.render(fileInfo.content)
let mergeReadingTime = {}
if(words_per_minute) {
mergeReadingTime.readingTime = calculateReadingTime(html, words_per_minute)
}
return {
...defaults,
...fileInfo.data,
updatedAt: formatDate(fileInfo.data.updated ? fileInfo.data.updated : stats.mtime),
createdAt: formatDate(fileInfo.data.created),
absolute_url: new URL(fileInfo.data.url || filename, defaults.baseUrl).href,
...mergeReadingTime,
filename,
url: fileInfo.data.url || "/" + filename,
html
}; // Return processed page info
}
/**
* Counts the number of words in an html string
* @param {string} html Expected to be the rendered html string from a markdown file
* @return {number} Number of whole words in the html string
*/
function get_word_count(html) {
let words = html.replace(/<[^>]*>/g," ");
words = words.replace(/\s+/g, ' ');
words = words.trim();
return words.split(" ").length
}
/**
* Returns the number of minutes an average reader would need to read a given number of words
* @param {number} word_count The number of words to read
* @return {number} The number of minutes it would take an average reader to read
*/
function get_reading_time(word_count, words_per_minute) {
const reading_time_in_minutes = Math.round(word_count / words_per_minute)
return reading_time_in_minutes
}
function calculateReadingTime(html, words_per_minute) {
const wordCount = get_word_count(html);
return get_reading_time(wordCount, words_per_minute);
}
function substituteHtml(template, page, pages, { markdownify_content_placeholder, markdownify_meta_placeholder } = {}) {
const htmlInsert = `<script>
window.markdownify = {
page: ${JSON.stringify(page)},
pages: ${JSON.stringify(pages)}
};
</script>`
const metaInsert = Object.keys(page)
.map(key => createMetaTag(page, key))
.join('');
return template
.replace(markdownify_content_placeholder, htmlInsert)
.replace(markdownify_meta_placeholder, metaInsert)
}
function makeSitemap(pages, { outputDir, sitemapTemplate, sitemapContentPlaceholder }) {
let sitemap_template;
if(sitemapTemplate) {
sitemap_template = fs.readFileSync(sitemapTemplate, 'utf-8');
} else {
sitemap_template = `<?xml version="1.0" encoding="utf-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">${sitemapContentPlaceholder}
</urlset>`;
}
const sitemap_string = pages
.map(file => {
return `
<url>
<loc>${file.absolute_url}</loc>
<priority>1.0</priority>
<lastmod>${new Date(file.updatedAt).toISOString()}</lastmod>
</url>`})
.join("");
let sitemap_html = sitemap_template.replace(
sitemapContentPlaceholder,
sitemap_string
);
makeFile(`sitemap.xml`, outputDir, sitemap_html)
}
function makeFeed(pages, { outputDir, defaults, feedTemplate, feedContentPlaceholder } = {}) {
let feed_template;
if(feedTemplate) {
feed_template = fs.readFileSync(feedTemplate, 'utf-8');
} else {
feed_template = `<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>${defaults.title}</title>
<author>${defaults.author}</author>
<link>${defaults.baseUrl}</link>
<description>${defaults.description}</description>
<atom:link href="${defaults.baseUrl}/feed.xml" rel="self" type="application/rss+xml"></atom:link>${feedContentPlaceholder}
</channel>
</rss>`;
}
const feed_string = pages
.filter(file => !['home', 'index', 'privacy-policy', 'about', 'contact', 'analytics', 'missing', '404'].includes(file.filename))
.filter(file => file.draft !== true)
.map(file => {
return `
<item>
<title>${file.title}</title>
<author>${file.author || defaults.author}</author>
<pubdate>${new Date(file.createdAt).toISOString()}</pubdate>
<description>${file.description || defaults.description}</description>
<link>${file.absolute_url}</link>
<guid ispermalink="true">${file.absolute_url}</guid>
</item>`;
})
.join("");
let feed_xml = feed_template.replace(feedContentPlaceholder, feed_string);
makeFile(`feed.xml`, outputDir, feed_xml)
}
function makeFile(filename, outputDirectory, content) {
const outputPath = path.join(outputDirectory, filename);
fs.ensureDirSync(path.dirname(outputPath)); // Create directories if they don't exist
fs.writeFileSync(outputPath, content); // Write HTML to output file
}
// Function to create a meta tag based on key and content
function createMetaTag(page, key) {
const content = page[key]
const dismiss = [
'html',
'url',
'baseUrl',
'created',
'updated',
'draft',
'filename',
'readingTime',
]
if(dismiss.includes(key)) { return ''; }
// Define templates for creating meta tags based on key
const templates = {
title: () => `
<title>${content}</title>
<meta name="pagename" content="${content}" />
<meta property="og:title" content="${content}" />
<meta name="twitter:title" content="${content}" />`,
author: () => `
<meta name="author" content="${content}" />
<meta property="article:author" content="${content}" />`,
description: () => `
<meta name="description" content="${content}" />
<meta property="og:description" content="${content}" />
<meta name="twitter:description" content="${content}" />`,
absolute_url: () => `
<meta name="url" content="${content}" />
<meta property="og:url" content="${content}" />
<meta name="identifier-URL" content="${content}" />`,
updatedAt: () => `
<meta property="article:modified_time" content="${new Date(content).toISOString()}" />`,
createdAt: () => `
<meta property="article:published_time" content="${new Date(content).toISOString()}" />`,
default: () => META_TAGS_WITH_NAME.includes(key) ? `
<meta name="${key}" content="${content}" />`: `
<meta property="${key}" content="${content}" />`
};
return (templates[key] || templates.default)();
}
function getFiles(dirPath, files_ = []) {
const files = fs.readdirSync(dirPath); // Read all files in directory
for (let file of files) { // Iterate through all files
let name = path.join(dirPath, file);
if (fs.statSync(name).isDirectory()) { // If the file is a directory, recurse
getFiles(name, files_);
} else {
// Only add the file to the list if it has a .md extension
if (path.extname(file) === '.md') {
files_.push(name); // If the file is not a directory, add it to the list
}
}
}
return files_; // Return the list of files
}
// Function to create an instance of markdownIt with required options and plugins
function createMarkdownItInstance() {
return MarkDownIt({ html: true, linkify: false, typographer: false })
.use(attrs)
.use(markdownToken)
.use(markdownFootnote);
}
export default markdownify;