-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.11tydata.js
94 lines (86 loc) · 2.27 KB
/
index.11tydata.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
'use strict';
const cheerio = require('cheerio');
const markdownItAnchor = require('markdown-it-anchor');
const markdownItPrism = require('markdown-it-prism');
const markdown = require('markdown-it')({
html: true,
linkify: true,
typographer: true
}).use(markdownItAnchor, {})
.use(markdownItPrism)
function language_array(language_tabs) {
let result = [];
for (let lang in language_tabs) {
if (typeof language_tabs[lang] === 'object') {
result.push(Object.keys(language_tabs[lang])[0]);
}
else {
result.push(language_tabs[lang]);
}
}
return JSON.stringify(result).split('"').join('"');
}
function image_tag(src, alt) {
return '<img src="slate/img/'+src+'" alt="'+(alt||'Image')+'">';
}
function logo_image_tag() {
return '<img src="slate/img/logo.png" alt="Logo" class="logo">';
}
function toc_data(content, headingLevel) {
const $ = cheerio.load(content);
const result = [];
let h1,h2,h3,h4,h5;
headingLevel = (headingLevel || 2);
$(':header').each(function(e){
const tag = $(this).get(0).tagName.toLowerCase();
const entry = {};
if (tag === 'h1') {
entry.id = $(this).attr('id');
entry.title = $(this).text();
entry.content = $(this).html();
entry.children = [];
h1 = entry;
result.push(entry);
}
if (tag === 'h2') {
let child = {};
child.id = $(this).attr('id');
entry.title = $(this).text();
child.content = $(this).html();
child.children = [];
h2 = child;
if (h1) h1.children.push(child);
}
if ((headingLevel >= 3) && (tag === 'h3')) {
let child = {};
child.id = $(this).attr('id');
entry.title = $(this).text();
child.content = $(this).html();
child.children = [];
h3 = child;
if (h2) h2.children.push(child);
}
if ((headingLevel >= 4) && (tag === 'h4')) {
let child = {};
child.id = $(this).attr('id');
entry.title = $(this).text();
child.content = $(this).html();
child.children = [];
h4 = child;
if (h3) h3.children.push(child);
}
});
return result;
}
function md(content) {
return markdown.render(content);
}
module.exports = {
layout: 'slate.ejs',
page_classes: '',
language_array,
image_tag,
logo_image_tag,
toc_data,
md
};