|
| 1 | +import marked from 'marked'; |
| 2 | +import { promises as fs } from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import yaml from 'yaml'; |
| 5 | + |
| 6 | +export default function contentPlugin(config, opts) { |
| 7 | + config.plugins.push(contentRollupPlugin({ ...config, ...opts })); |
| 8 | +} |
| 9 | +contentPlugin.rollup = contentRollupPlugin; |
| 10 | + |
| 11 | +async function tree(dir, prefix = '') { |
| 12 | + const entries = await fs.readdir(dir, { withFileTypes: true }); |
| 13 | + const list = await Promise.all( |
| 14 | + entries.map(entry => { |
| 15 | + if (entry[0] === '.') return; |
| 16 | + const name = (prefix ? prefix + '/' : '') + entry.name; |
| 17 | + if (entry.isDirectory()) return tree(path.join(dir, entry.name), name); |
| 18 | + return name; |
| 19 | + }) |
| 20 | + ); |
| 21 | + return list.flat().filter(Boolean); |
| 22 | +} |
| 23 | + |
| 24 | +const FRONT_MATTER_REG = /^\s*---\n\s*([\s\S]*?)\s*\n---\n/i; |
| 25 | +const TITLE_REG = /^\s*#\s+(.+)\n+/; |
| 26 | +async function getMeta(filename) { |
| 27 | + let meta = {}; |
| 28 | + let content = await fs.readFile(filename, 'utf-8'); |
| 29 | + content = content.replace(FRONT_MATTER_REG, (s, fm) => { |
| 30 | + meta = yaml.parse('---\n' + fm.replace(/^/gm, ' ') + '\n') || meta; |
| 31 | + return ''; |
| 32 | + }); |
| 33 | + content = content.replace(TITLE_REG, s => { |
| 34 | + if (!meta.title) meta.title = s; |
| 35 | + return ''; |
| 36 | + }); |
| 37 | + content = decodeHtmlEntities(marked(content)); |
| 38 | + if (!meta.description) { |
| 39 | + let stripped = content.replace(/(?:<(figcaption)[^>]*?>.*?<\/\1>|<.*?>|(?:^|\n)>)/g, '').trim(); |
| 40 | + let desc = stripped.match(/[^\n]+/g)[0]; |
| 41 | + if (desc && desc.length > 200) desc = desc.slice(0, 199) + '…'; |
| 42 | + meta.description = desc; |
| 43 | + } |
| 44 | + if ( |
| 45 | + meta.published && |
| 46 | + meta.updated && |
| 47 | + meta.published.replace(/:\d\d:\d\d/, '') === meta.updated.replace(/:\d\d:\d\d/, '') |
| 48 | + ) |
| 49 | + delete meta.updated; |
| 50 | + if (meta.description === meta.meta_description) delete meta.meta_description; |
| 51 | + if (meta.title === meta.meta_title) delete meta.meta_title; |
| 52 | + for (let i in meta) if (i[0] === '.' || i[0] === '_') delete meta[i]; |
| 53 | + // we could return all the metadata here, but it's currently unused: |
| 54 | + // return meta; |
| 55 | + return { |
| 56 | + nav: meta.nav || meta.title |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +function decodeHtmlEntities(html) { |
| 61 | + return html.replace(/&(?:#(\d+)|times|apos|quot|amp);/g, (s, n, t) => { |
| 62 | + switch (t) { |
| 63 | + case 'times': |
| 64 | + return '×'; |
| 65 | + case 'apos': |
| 66 | + return 'ʼ'; |
| 67 | + case 'quot': |
| 68 | + return '"'; |
| 69 | + case 'amp': |
| 70 | + return '&'; |
| 71 | + } |
| 72 | + return String.fromCharCode(n); |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * markdown blog/content plugin for Rollup / WMR |
| 78 | + */ |
| 79 | +function contentRollupPlugin({ cwd, prod, ...opts }) { |
| 80 | + return { |
| 81 | + name: 'content', |
| 82 | + async resolveId(id, importer) { |
| 83 | + if (id[0] === '\0' || !id.startsWith('content:')) return; |
| 84 | + id = id.slice(8); |
| 85 | + if (importer) importer = importer.replace(/^[\0\b]\w+:/g, ''); |
| 86 | + let resolved = await this.resolve(id, importer, { skipSelf: true }); |
| 87 | + if (!resolved) { |
| 88 | + const r = path.join(path.dirname(importer), id); |
| 89 | + const s = await fs.stat(r).catch(() => null); |
| 90 | + if (s && s.isDirectory()) resolved = { id: r }; |
| 91 | + } |
| 92 | + if (resolved) return '\0content:' + resolved.id.replace(/\/\.$/, ''); |
| 93 | + }, |
| 94 | + async load(id) { |
| 95 | + if (!id.startsWith('\0content:')) return; |
| 96 | + id = path.resolve(cwd || '.', id.slice(9)); |
| 97 | + const files = (await tree(id)).filter(file => file.endsWith('.md')); |
| 98 | + const data = await Promise.all( |
| 99 | + files.map(async file => { |
| 100 | + const { slug, ...meta } = await getMeta(path.resolve(id, file)); |
| 101 | + return { name: slug || file.replace(/\.md$/, ''), ...meta }; |
| 102 | + }) |
| 103 | + ); |
| 104 | + data.sort((a, b) => +new Date(b.published) - +new Date(a.published)); |
| 105 | + |
| 106 | + let imports = ''; |
| 107 | + |
| 108 | + const serializeItem = item => { |
| 109 | + const url = 'markdown:./' + path.posix.relative(path.dirname(id), path.resolve(id, item.name)) + '.md'; |
| 110 | + imports += `import ${JSON.stringify(url)};\n`; |
| 111 | + |
| 112 | + let str = '{ '; |
| 113 | + for (let i in item) if (item[i] != null) str += `${i}: ${JSON.stringify(item[i])}, `; |
| 114 | + return str + '}'; |
| 115 | + }; |
| 116 | + |
| 117 | + const code = 'export default [' + data.map(serializeItem).join(',\n') + '];'; |
| 118 | + return imports + code; |
| 119 | + } |
| 120 | + }; |
| 121 | +} |
0 commit comments