|
| 1 | +import markdownIt from 'markdown-it'; |
| 2 | +import markdownItAnchor from 'markdown-it-anchor'; |
| 3 | +import GithubSlugger from 'github-slugger'; |
| 4 | +import { roadmaps } from './roadmaps.config.mjs'; |
| 5 | + |
| 6 | +// One slugger per rendered document (GitHub-compatible slugs + dedup). Reassigned |
| 7 | +// before each roadmap render so anchors match the source README exactly. |
| 8 | +let slugger = new GithubSlugger(); |
| 9 | + |
| 10 | +const md = markdownIt({ html: true, linkify: true, typographer: false }).use( |
| 11 | + markdownItAnchor, |
| 12 | + { |
| 13 | + slugify: (s) => slugger.slug(s), |
| 14 | + tabIndex: false, |
| 15 | + permalink: markdownItAnchor.permalink.linkInsideHeader({ |
| 16 | + symbol: '#', |
| 17 | + placement: 'after', |
| 18 | + class: 'ps-anchor', |
| 19 | + ariaHidden: true |
| 20 | + }) |
| 21 | + } |
| 22 | +); |
| 23 | + |
| 24 | +// Rewrite a single relative URL from a roadmap README to a working site/GitHub URL. |
| 25 | +function rewriteUrl(url, roadmap) { |
| 26 | + if (!url) return url; |
| 27 | + if (url.startsWith('#')) return url; // in-page anchor |
| 28 | + if (/^(https?:)?\/\//i.test(url) || url.startsWith('mailto:') || url.startsWith('data:')) return url; |
| 29 | + const clean = url.replace(/^\.\//, ''); |
| 30 | + if (clean.startsWith('assets/')) return `/${roadmap.slug}/${clean}`; // local copied asset |
| 31 | + return `https://github.com/${roadmap.repo}/blob/${roadmap.branch}/${clean}`; // repo file → GitHub |
| 32 | +} |
| 33 | + |
| 34 | +function rewriteLinks(html, roadmap) { |
| 35 | + return html |
| 36 | + .replace(/(<a\b[^>]*\shref=")([^"]*)(")/g, (_m, a, u, c) => a + rewriteUrl(u, roadmap) + c) |
| 37 | + .replace(/(<img\b[^>]*\ssrc=")([^"]*)(")/g, (_m, a, u, c) => a + rewriteUrl(u, roadmap) + c); |
| 38 | +} |
| 39 | + |
| 40 | +export default function (eleventyConfig) { |
| 41 | + // Global chrome assets. |
| 42 | + eleventyConfig.addPassthroughCopy({ 'src/assets': 'assets' }); |
| 43 | + |
| 44 | + // Custom-domain marker. Actions deploys do NOT auto-create a CNAME file |
| 45 | + // (GitHub Docs), so we ship one to keep proofstone.dev bound on every deploy. |
| 46 | + eleventyConfig.addPassthroughCopy({ 'src/CNAME': 'CNAME' }); |
| 47 | + |
| 48 | + // Per-roadmap assets fetched into .content/<slug>/assets → /<slug>/assets/… |
| 49 | + for (const r of roadmaps) { |
| 50 | + eleventyConfig.addPassthroughCopy({ [`.content/${r.slug}/assets`]: `${r.slug}/assets` }); |
| 51 | + } |
| 52 | + |
| 53 | + // Render a roadmap README to HTML with GitHub-compatible anchors + rewritten links. |
| 54 | + eleventyConfig.addFilter('roadmapMarkdown', (content, roadmap) => { |
| 55 | + slugger = new GithubSlugger(); // reset dedup state per document |
| 56 | + const html = md.render(content || ''); |
| 57 | + return rewriteLinks(html, roadmap); |
| 58 | + }); |
| 59 | + |
| 60 | + eleventyConfig.addWatchTarget('./src/assets/'); |
| 61 | + |
| 62 | + return { |
| 63 | + dir: { input: 'src', includes: '_includes', data: '_data', output: '_site' }, |
| 64 | + markdownTemplateEngine: 'njk', |
| 65 | + htmlTemplateEngine: 'njk' |
| 66 | + }; |
| 67 | +} |
0 commit comments