-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
112 lines (96 loc) · 2.89 KB
/
parser.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
const fs = require('fs')
const path = require('path')
const glob = require('glob')
const matter = require('gray-matter')
const MarkdownIt = require('markdown-it')
const { compile } = require('mdsvex');
const mili = require('./images.js')
const hljs = require('highlight.js')
const typeset = require('typeset')
const urlMetadata = require('url-metadata')
const svelte = require('svelte/compiler');
let routesPath = path.join(process.cwd(), `/src/routes`)
let dataPath = path.join(process.cwd(), `/src/data`)
// Read content files
const source = () => new Promise((resolve, reject) => {
let pattern = path.join(process.cwd(), '/content/*.md')
glob(pattern, {}, function (err, result) {
if (err) { return reject(err, err.stack) }
return resolve(result)
})
})
const writeErr = (err) => {
err ? console.error(err) : () => {}
}
// new content pipeline
const getFile = (path) => new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
reject(err)
} else {
resolve(data)
}
})
})
const generateResources = async () => {
let links = await getFile(path.join(process.cwd(), '/content/resources.md'))
let linkPromises = links
.replace(/\n/gi, '')
.split('- ')
.filter(url => typeof url === 'string')
.map(url => urlMetadata(`${encodeURI(url)}`, {descriptionLength: 1500}))
let results = await Promise.allSettled(linkPromises)
let data = results
.filter(result => result.status === 'fulfilled')
.map((result) => {
return {
url: result.value.url,
title: result.value.title,
description: result.value.description,
keywords: result.value.keywords
}
})
fs.writeFile(`${dataPath}/resources.json`, JSON.stringify(data), writeErr)
}
const svexify = async (paths) => {
let files = await Promise.allSettled(paths.map(path => getFile(path)))
let contents = files.map(file => file.value)
let parsed = await Promise.allSettled(contents.map(content => compile(content)))
let markups = parsed
.map(parse => parse.value)
.filter(file => file.data.fm != undefined)
.map(file => {
file.data.fm.timestamp = Date.parse(file.data.fm.date)
return file
})
return markups
}
const orderMostRecent = (a, b) => b.data.fm.timestamp - a.data.fm.timestamp
const writeComponents = (files) => {
files
.sort(orderMostRecent)
.forEach(file => {
if (file.data.fm.slug) {
fs.writeFile(`${routesPath}/${file.data.fm.slug}.svelte`, file.code, writeErr)
}
})
return files
}
const writeSummaries = (files) => {
let fileMetas = files
.sort(orderMostRecent)
.map((file) => file.data.fm)
fs.writeFile(`${dataPath}/meta.json`, JSON.stringify(fileMetas), writeErr)
return files
}
source()
.then(svexify)
.then(writeComponents)
.then(writeSummaries)
.catch(e => {
console.error(e)
})
generateResources()
.catch(e => {
console.error(e)
})