54 lines
1.5 KiB
JavaScript
54 lines
1.5 KiB
JavaScript
const fs = require('node:fs/promises')
|
|
const path = require('node:path')
|
|
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight')
|
|
|
|
module.exports = function (eleventyConfig) {
|
|
// Copy static to output folder as-is.
|
|
eleventyConfig.addPassthroughCopy('story/static')
|
|
|
|
eleventyConfig.addPlugin(syntaxHighlight)
|
|
|
|
eleventyConfig.addWatchTarget('./enterprise/')
|
|
|
|
eleventyConfig.addAsyncShortcode(
|
|
'include-code',
|
|
async function (relativePath, highlightLanguage = 'js') {
|
|
const { inputPath } = this.page
|
|
const absPath = path.resolve(
|
|
__dirname,
|
|
path.dirname(inputPath),
|
|
relativePath
|
|
)
|
|
const files = await loadFileOrFiles(absPath)
|
|
return files
|
|
.map(({ name, content }) => {
|
|
return [
|
|
`<p><strong>${name}</strong></p>`,
|
|
eleventyConfig.javascriptFunctions.highlight(
|
|
highlightLanguage,
|
|
content
|
|
),
|
|
].join('')
|
|
})
|
|
.join('')
|
|
}
|
|
)
|
|
}
|
|
|
|
async function loadFileOrFiles(absPath) {
|
|
const stats = await fs.stat(absPath)
|
|
if (stats.isFile()) {
|
|
const content = await fs.readFile(absPath, { encoding: 'utf-8' })
|
|
return [{ name: path.basename(absPath), content }]
|
|
}
|
|
const files = await fs.readdir(absPath)
|
|
const result = []
|
|
for (const name of files) {
|
|
const content = await fs.readFile(path.resolve(absPath, name), {
|
|
encoding: 'utf-8',
|
|
})
|
|
result.push({ name, content })
|
|
}
|
|
return result
|
|
}
|