forked from runelite/runelite.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-feed.js
More file actions
140 lines (126 loc) · 3.13 KB
/
Copy pathgenerate-feed.js
File metadata and controls
140 lines (126 loc) · 3.13 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
import * as jstoxml from 'jstoxml'
import fs from 'fs'
import path from 'path'
import MarkdownIt from 'markdown-it'
import frontmatter from 'front-matter'
import hero from './src/_data/hero'
const postsFolder = path.join('src', '_posts')
const now = new Date()
// Prepare markdown renderer to be xhtml-compatible
const md = MarkdownIt({
html: true,
linkify: true,
typographer: true,
xhtmlOut: true
})
// Read each file in posts folder and convert it to json-formatted meta tags
const posts = fs.readdirSync(postsFolder)
.map(fileName => {
// setup path to file
const filePath = path.join(postsFolder, fileName)
// read the content of the file
const fileContent = fs.readFileSync(filePath, 'utf-8')
// extract front-matter context
const frontMatterContext = frontmatter(fileContent)
// remove cd and extension
fileName = fileName.match(/([\w\d-.]+)\.md/)[1]
// extract year and path
const tokenizedFilename = fileName.match(/^(\d{4}-\d{2}-\d{2})(.*)/)
// validation
if (!tokenizedFilename && !tokenizedFilename[1]) throw new Error('no ^YYYY-MM-DD date in blog filename')
// extract date, title and description
const dateString = tokenizedFilename[1]
const pathString = tokenizedFilename[2]
const title = frontMatterContext.attributes.title
const description = frontMatterContext.attributes.description
const author = frontMatterContext.attributes.author
// create required metadata
const date = new Date(dateString)
const link = `${hero.url}/blog/show/${dateString + pathString}`
// build content from markdown
const content = md.render(frontMatterContext.body)
return {
entry: [
{
id: link
},
{
link: {
_attrs: {
href: link
}
}
},
{
title
},
{
summary: description
},
{
updated: date.toISOString()
},
{
author: {
name: author
}
},
{
_name: 'content',
_attrs: {
type: 'xhtml'
},
_content: [
{
div: {
_attrs: {
xmlns: 'http://www.w3.org/1999/xhtml'
},
div: content
}
}
]
}
]
}
})
// Order from newest to oldest
.reverse()
// Build the Atom XML from JSON
const xml = jstoxml.toXML({
_name: 'feed',
_attrs: {
xmlns: 'http://www.w3.org/2005/Atom'
},
_content: [
{
title: hero.title
},
{
subtitle: hero.description
},
{
link: {
_attrs: {
href: hero.url
}
}
},
{
link: {
_attrs: {
href: 'https://raw.githubusercontent.com/runelite/runelite.net/gh-pages/atom.xml',
rel: 'self'
}
}
},
{
id: hero.url + '/'
},
{
updated: now.toISOString()
}
].concat(posts)
}, {header: true, indent: ' '})
// Write feed to build folder
fs.writeFileSync(path.join('build', 'atom.xml'), xml)