-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
166 lines (150 loc) · 5.14 KB
/
.eleventy.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
require('dotenv').config()
const eleventyNavigationPlugin = require('@11ty/eleventy-navigation')
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight')
const markdownIt = require('markdown-it')
const markdownItAnchor = require('markdown-it-anchor')
const markdownItReplaceLink = require('markdown-it-replace-link')
const pluginTOC = require('eleventy-plugin-toc')
const cheerio = require('cheerio')
const memoize = require('lodash.memoize')
const fetch = require('node-fetch')
const repositoryUrl = require('./package.json').repository.url
module.exports = function (eleventyConfig) {
const buildstamp = process.env.MODE === 'gh_pages' ? Date.now() + '/' : ''
eleventyConfig.addGlobalData('buildstamp', buildstamp)
eleventyConfig.setWatchJavaScriptDependencies(false)
// Navigation
eleventyConfig.addPlugin(eleventyNavigationPlugin)
// Table of contents
eleventyConfig.addPlugin(pluginTOC, {
tags: ['h2', 'h3'],
wrapperClass: 'docs-toc__nav',
})
// Syntax highlighting
eleventyConfig.addPlugin(syntaxHighlight, {
alwaysWrapLineHighlights: false,
})
const highlighter = eleventyConfig.markdownHighlighter
eleventyConfig.addMarkdownHighlighter((str, language) => {
if (language === 'mermaid') {
return `<pre class="mermaid">${str}</pre>`
}
return highlighter(str, language)
})
// Headings
const position = { false: 'push', true: 'unshift' }
const renderPermalink = (slug, opts, state, idx) => {
const space = () =>
Object.assign(new state.Token('text', '', 0), {
content: ' ',
})
const linkTokens = [
Object.assign(new state.Token('link_open', 'a', 1), {
attrs: [
['class', opts.permalinkClass],
['href', state.env.permalink + opts.permalinkHref(slug, state)],
],
}),
Object.assign(new state.Token('html_block', '', 0), {
content: '<span aria-label="" class="header-anchor__symbol">#</span>',
}),
new state.Token('link_close', 'a', -1),
]
if (opts.permalinkSpace) {
linkTokens[position[!opts.permalinkBefore]](space())
}
state.tokens[idx + 1].children[position[opts.permalinkBefore]](
...linkTokens
)
}
eleventyConfig.setLibrary(
'md',
markdownIt({
html: true,
linkify: true,
replaceLink: (link, env) => {
// Convert relative links to absolute links
const base = process.env.MODE === 'gh_pages' ? '/liquid' : ''
if (link.startsWith('./')) {
const splitted = env.page.url.split('/')
splitted.splice(splitted.length - 1, 0, link.substr(2))
return base + splitted.join('/')
}
if (link.startsWith('../')) {
const splitted = env.page.url.split('/')
splitted.splice(splitted.length - 2, 1, link.substr(3))
return base + splitted.join('/')
}
return link
},
})
.use(markdownItAnchor, {
permalink: true,
renderPermalink,
})
.use(markdownItReplaceLink)
)
const passthrough = {
'src/docs/assets/favicon.ico': 'favicon.ico',
'src/docs/assets': `${buildstamp}assets`,
'node_modules/@emdgroup-liquid/liquid/dist/css': `${buildstamp}liquid/css`,
'node_modules/@emdgroup-liquid/liquid/dist/esm/*.js': `${buildstamp}liquid/esm`,
'node_modules/@emdgroup-liquid/liquid/dist/esm/polyfills*.js': `${buildstamp}liquid/esm/polyfills`,
'node_modules/@emdgroup-liquid/liquid/dist/liquid/assets': `${buildstamp}liquid/esm/assets`,
}
if (buildstamp) {
passthrough['dist_docs/docs.css'] = `${buildstamp}docs.css`
}
eleventyConfig.addPassthroughCopy(passthrough)
// Memoized serch index filter for headings
eleventyConfig.addNunjucksFilter(
'memoizedHeadings',
memoize((value) =>
Array.from(cheerio.load(value)('h2, h3').contents())
.filter((elem) => elem.type === 'text')
.map((elem) => elem.data)
.join(' ')
.replaceAll(/(\n|\r)/g, ' ')
)
)
// Contributors short code (used in layout.njk)
eleventyConfig.addNunjucksAsyncShortcode(
'contributors',
async function (inputPath) {
const path = inputPath.replace('/readme.md', '').replace('/index.md', '')
if (!repositoryUrl.includes('github.com')) return
let repoPathName = new URL(repositoryUrl.replace('git@', '')).pathname
if (repoPathName.endsWith('.git'))
repoPathName = repoPathName.slice(0, -4)
let commits
try {
const res = await fetch(
`https://api.github.com/repos/${repoPathName}/commits?path=${path}`,
{
headers: { Authorization: `token ${process.env.GH_TOKEN}` },
}
)
commits = await res.json()
} catch (err) {
console.warn(`Failed fetching contributors for path ${path}`, err)
return '[]'
}
if (!Array.isArray(commits)) {
return '[]'
}
return JSON.stringify([
...new Set(
commits.map((entry) => entry.author.html_url.split('/').pop())
),
])
}
)
return {
dir: {
input: './src',
output: './dist_docs',
includes: './docs/includes',
},
templateFormats: ['md', 'njk'],
}
}