-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path.eleventy.js
More file actions
190 lines (166 loc) · 5.61 KB
/
.eleventy.js
File metadata and controls
190 lines (166 loc) · 5.61 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import fs from "fs"
import path from "path"
import dotenv from "dotenv"
import { DateTime } from "luxon"
import highlight from "highlight.js"
import highlightDiff from "highlightjs-code-diff"
import highlightjsLines from "./lib/highlightjs-lines.js"
import markdownIt from "markdown-it"
import anchor from "markdown-it-anchor"
import bracketedSpans from "markdown-it-bracketed-spans"
import attrs from "markdown-it-attrs"
import toc from "markdown-it-table-of-contents"
import mark from "markdown-it-mark"
import { eleventyImageTransformPlugin } from "@11ty/eleventy-img"
import rssPlugin from "@11ty/eleventy-plugin-rss"
import link from "./src/_data/link.json" with {type: 'json'}
dotenv.config()
export default function (eleventyConfig) {
const isDevelopment = process.env.NODE_ENV === "development"
const hljs = highlightjsLines(highlightDiff(highlight))
const markdownItConfig = markdownIt({
html: true,
linkify: true,
typographer: true,
highlight: function (str, lang) {
const processLang = lang.replace("diff:", "")
if (lang && hljs.getLanguage(processLang)) {
try {
const returnValue = hljs.highlight(str, { language: lang }).value
return returnValue
} catch (__) { }
}
return ""
},
})
.use(anchor, {
permalink: anchor.permalink.linkAfterHeader({
style: "visually-hidden",
assistiveText: (title) => `Link to heading “${title}”`,
visuallyHiddenClass: "sr-only",
wrapper: ['<div class="header-wrapper">', '</div>'],
}),
})
.use(bracketedSpans)
.use(attrs, {
leftDelimiter: "{",
rightDelimiter: "}",
allowedAttributes: [],
})
.use(toc, {
includeLevel: [1, 2, 3],
containerHeaderHtml: `<div class="toc-container-header">Table of Contents</div>`,
})
.use(mark)
eleventyConfig.setLibrary("md", markdownItConfig)
eleventyConfig.addGlobalData("isDevelopment", isDevelopment)
eleventyConfig.addPlugin(rssPlugin, {
posthtmlRenderOptions: {
closingSingleTag: "default",
},
})
eleventyConfig.addPlugin(eleventyImageTransformPlugin, {
outputDir: "./images"
});
eleventyConfig.addPassthroughCopy({
"src/assets/*.pdf": "assets",
"src/assets/images": "images",
"src/admin/*": "admin",
"settings.json": "settings.json",
"src/robots.txt": "robots.txt",
"src/*.xsl": "/"
})
if (isDevelopment) {
eleventyConfig.addPassthroughCopy({
"src/assets/js": "js",
})
}
eleventyConfig.addFilter("postDate", (dateObj) => {
if (typeof dateObj !== "object") dateObj = new Date(dateObj)
return DateTime.fromJSDate(dateObj).toLocaleString(DateTime.DATE_MED)
})
eleventyConfig.addFilter("reverseGroupedPosts", (object) =>
Object.entries(object).sort((a, b) => b[0] - a[0])
)
eleventyConfig.addFilter("htmlDateString", (dateObj) => {
let date = new Date(dateObj)
return date.toISOString()
})
eleventyConfig.addFilter("convertToValidURL", (url) => {
if (url.startsWith('http')) return url
const validURL = new URL(url.replace("\/assets\/",""), (isDevelopment) ? "http://localhost" : link.website)
return validURL
})
eleventyConfig.addFilter("postYear", (dateObj) => {
return DateTime.fromJSDate(dateObj).toLocaleString({ year: "numeric" })
})
eleventyConfig.addFilter("sliceRecent", (array) => {
return array.slice(0, 3)
})
eleventyConfig.addFilter("clipText", (string, size = 12, separator = "-") => {
return string.split(separator).splice(0, size).join(separator)
})
eleventyConfig.addFilter("serializeTitle", (string, yearSplice = 2) => {
const titleParts = string.split("-")
const [year, ...title] = titleParts
const titleInitials = title.map((title) => title.split("")[0]).join("")
const yearSuffix = year.slice(-yearSplice)
return `${yearSuffix}-${titleInitials}`
})
eleventyConfig.addFilter("development", (link) => {
return isDevelopment ? "/" : link
})
eleventyConfig.addFilter("breakLine", (string, cutAt = 3, maxSize = 30) => {
const titleWords = string.split(" ")
const titleLength = string.length
const titlePreview = titleWords.slice(0, cutAt).join(" ")
const titleRemaining = titleWords.slice(cutAt).join(" ")
const hasTitleRemaining = !!titleRemaining
const formattedTitleWithBreak = hasTitleRemaining
? `${titlePreview}<br/>${titleRemaining}`
: titlePreview
return titleLength <= maxSize || !hasTitleRemaining
? string
: formattedTitleWithBreak
})
eleventyConfig.addFilter("svg", (fileName, index) => {
const filePath = path.join(process.cwd(), "src/assets/svg", fileName)
try {
let svgContent = fs.readFileSync(filePath, "utf8")
if (index) {
svgContent = svgContent.replace(
"<svg",
`<svg data-animation="fade-in" data-delay=${index}`
)
}
return svgContent
} catch (error) {
return `<!-- SVG ${fileName} not found -->`
}
})
eleventyConfig.addFilter("renderMarkdown", (text) => {
return markdownItConfig.render(text)
})
eleventyConfig.addFilter("renderMarkdownInline", (text) => {
return markdownItConfig.renderInline(text)
})
eleventyConfig.addFilter("featuredDate", (string) => {
let date = new Date(string)
return date.toLocaleDateString("en-us", {
year: "numeric",
month: "short",
day: "numeric",
})
})
return {
templateFormats: ["md", "njk", "html", "liquid"],
markdownTemplateEngine: "liquid",
htmlTemplateEngine: "njk",
dataTemplateEngine: "njk",
dir: {
data: "_data",
input: "src",
output: "public",
},
}
}