-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy-utils.js
172 lines (151 loc) · 4.32 KB
/
.eleventy-utils.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
const markdownIt = require("markdown-it");
const hljs = require("highlight.js");
const numbers = ["", "one", "two", "three", "four", "five", "six"];
function applyRendererRules(markdownRenderer) {
markdownRenderer.renderer.rules.heading_open = (
tokens,
idx,
options,
env,
renderer
) => {
const token = tokens[idx];
const level = numbers[parseInt(token.tag.substr(1), 10)];
return `<${tokens[idx].tag} class="heading heading--${level}">`;
};
markdownRenderer.renderer.rules.paragraph_open = () => {
return `<p class="paragraph">`;
};
markdownRenderer.renderer.rules.hr = () => {
return `<hr class="rule" />`;
};
markdownRenderer.renderer.rules.link_open = (
tokens,
index,
options,
env,
renderer
) => {
const token = tokens[index];
return `<a class="anchor anchor--primary" ${renderer.renderAttrs(token)}>`;
};
markdownRenderer.renderer.rules.blockquote_open = (
tokens,
index,
options,
env,
renderer
) => {
const token = tokens[index];
return `<blockquote class="blockquote">`;
};
markdownRenderer.renderer.rules.table_open = () => {
return `<table class="table">`;
};
const originalImageRule = markdownRenderer.renderer.rules.image;
markdownRenderer.renderer.rules.image = (
tokens,
idx,
options,
env,
renderer
) => {
const token = tokens[idx];
// Add class attribute
token.attrs.push(["class", "image"]);
// Do the original render
return originalImageRule(tokens, idx, options, env, renderer);
};
markdownRenderer.renderer.rules.mark_open = () => {
return `<mark class="mark">`;
};
markdownRenderer.renderer.rules.ordered_list_open = () => {
return '<ol class="ordered-list">';
};
markdownRenderer.renderer.rules.bullet_list_open = () => {
return '<ul class="unordered-list">';
};
}
function getAllPublishedPosts(collectionApi) {
return collectionApi
.getAll()
.filter(
(content) => isContentPublished(content) && isContentType(content, "post")
)
.map((post) => getCardModelFromPost(post));
}
function getAllPublishedProjects(collectionApi) {
return collectionApi
.getAll()
.filter(
(content) =>
isContentPublished(content) && isContentType(content, "project")
)
.map((project) => getCardModelFromProject(project));
}
function getCardModelFromPost({ data }) {
return {
category_name: data.category_name,
category_url: data.category_url,
is_external: data.is_external || false,
publish_date_label: data.publish_date_label,
publish_date_datetime: data.publish_date_datetime,
tags: (data.tags ?? []).slice(0),
teaser: data.description,
title: data.title,
url: data.url,
};
}
function getCardModelFromProject({ data }) {
return {
publish_date_label: data.publish_date_label,
publish_date_datetime: data.publish_date_datetime,
teaser: data.description,
title: data.title,
url: data.url,
};
}
function getMarkdownRenderer() {
const markdownRenderer = markdownIt({
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
const highlightedText = hljs.highlight(str, {
language: lang,
ignoreIllegals: true,
}).value;
return `<pre class="hljs"><code>${highlightedText}</code></pre>`;
} catch (__) {}
}
const highlightedText = markdownRenderer.utils.escapeHtml(str);
return `<pre class="hljs"><code>${highlightedText}</code></pre>`;
},
})
.use(require("markdown-it-sub"))
.use(require("markdown-it-sup"))
.use(require("markdown-it-footnote"))
.use(require("markdown-it-deflist"))
.use(require("markdown-it-abbr"))
.use(require("markdown-it-ins"))
.use(require("markdown-it-mark"));
applyRendererRules(markdownRenderer);
return markdownRenderer;
}
function isContentPublished(content) {
return content.data.status === "published";
}
function isContentType(content, type) {
return content.data.layout === type;
}
function sortContentMostRecent(content1, content2) {
return content2.publish_date_datetime - content1.publish_date_datetime;
}
module.exports = {
applyRendererRules,
getAllPublishedPosts,
getAllPublishedProjects,
getCardModelFromPost,
getMarkdownRenderer,
isContentPublished,
sortContentMostRecent,
};