|
1 | | -import techdoc from "eleventy-plugin-techdoc"; |
| 1 | +import syntaxHighlight from "@11ty/eleventy-plugin-syntaxhighlight"; |
| 2 | +import markdownIt from "markdown-it"; |
| 3 | +import anchor from "markdown-it-anchor"; |
2 | 4 |
|
3 | | -export default function(eleventyConfig) { |
4 | | - eleventyConfig.addPlugin(techdoc, { |
5 | | - site: { |
6 | | - name: "RestClient.Net", |
7 | | - url: "https://restclient.net", |
8 | | - description: "The safest way to make REST calls in C#. Built with functional programming, type safety, and modern .NET patterns.", |
9 | | - }, |
10 | | - features: { |
11 | | - blog: true, |
12 | | - docs: true, |
13 | | - darkMode: true, |
14 | | - i18n: true, |
15 | | - }, |
16 | | - i18n: { |
17 | | - defaultLanguage: 'en', |
18 | | - languages: ['en', 'zh'], |
19 | | - }, |
| 5 | +const origin = process.env.SITE_URL || "https://melbournedeveloper.github.io"; |
| 6 | +const prefix = "/" + (process.env.SITE_PATH_PREFIX || "").replace(/^\/+|\/+$/g, "") + "/"; |
| 7 | +const basePath = prefix === "//" ? "/" : prefix; |
| 8 | +const absoluteUrl = value => new URL(String(value || "/").replace(/^\//,""), new URL(basePath,origin)).href; |
| 9 | +export default function (config) { |
| 10 | + config.addPlugin(syntaxHighlight); |
| 11 | + config.setLibrary("md", markdownIt({html:true,linkify:true}).use(anchor, {slugify:s=>s.toLowerCase().replace(/\s+/g,"-").replace(/[^\p{L}\p{N}_-]/gu,"")})); |
| 12 | + config.addPassthroughCopy("src/assets"); |
| 13 | + config.addPassthroughCopy({"src/api/reference/api.json":"api/reference/api.json", "src/api/reference/schema.json":"api/reference/schema.json"}); |
| 14 | + config.addCollection("posts", api=>api.getFilteredByGlob("src/blog/*.md").sort((a,b)=>b.date-a.date)); |
| 15 | + config.addCollection("zhposts", api=>api.getFilteredByGlob("src/zh/blog/*.md").sort((a,b)=>b.date-a.date)); |
| 16 | + config.addCollection("journalArchives", api => { |
| 17 | + const archives = []; |
| 18 | + for (const lang of ["en", "zh"]) { |
| 19 | + const prefix = lang === "zh" ? "/zh" : ""; |
| 20 | + const posts = api.getFilteredByGlob(`src${prefix}/blog/*.md`).sort((a,b)=>b.date-a.date); |
| 21 | + for (const kind of ["tags", "categories"]) { |
| 22 | + archives.push({url:`${prefix}/blog/${kind}/`,lang,title:lang === "zh" ? (kind === "tags" ? "博客主题" : "博客分类") : (kind === "tags" ? "Journal topics" : "Journal categories"),posts}); |
| 23 | + const values = [...new Set(posts.flatMap(p=>kind === "tags" ? (p.data.tags || []).filter(t=>!["post","posts"].includes(t)) : p.data.category ? [p.data.category] : []))]; |
| 24 | + for (const value of values) archives.push({url:`${prefix}/blog/${kind}/${value.toLowerCase().replace(/[^a-z0-9]+/g,"-")}/`,lang,title:(lang === "zh" ? "博客 / " : "Journal / ")+value,posts:posts.filter(p=>kind === "tags" ? (p.data.tags || []).includes(value) : p.data.category === value)}); |
| 25 | + } |
| 26 | + } |
| 27 | + return archives; |
20 | 28 | }); |
21 | | - |
22 | | - eleventyConfig.addPassthroughCopy("src/assets"); |
23 | | - |
24 | | - return { |
25 | | - dir: { input: "src", output: "_site" }, |
26 | | - markdownTemplateEngine: "njk", |
27 | | - pathPrefix: "/RestClient.Net/", |
28 | | - }; |
| 29 | + config.addCollection("publicPages", api=>api.getAll().filter(p=>p.url && !p.data.eleventyExcludeFromCollections)); |
| 30 | + config.addFilter("isoDate", value=>new Date(value).toISOString()); |
| 31 | + config.addFilter("dateFormat", value=>new Date(value).toLocaleDateString("en",{year:"numeric",month:"long",day:"numeric",timeZone:"UTC"})); |
| 32 | + config.addFilter("xmlEscape", value=>String(value??"").replace(/[<>&"']/g,c=>({"<":"<",">":">","&":"&",'"':""","'":"'"}[c]))); |
| 33 | + config.addFilter("absoluteUrl", absoluteUrl); |
| 34 | + config.addFilter("translationUrl", (url,lang,pages=[])=>{const route=(lang==="zh"?"/zh":"")+url.replace(/^\/zh(?=\/)/,"");return pages.some(p=>p.url===route)?route:(lang==="zh"?"/zh/":"/");}); |
| 35 | + config.addFilter("hasTranslation",(url,lang,pages=[])=>pages.some(p=>p.url===(lang==="zh"?"/zh":"")+url.replace(/^\/zh(?=\/)/,""))); |
| 36 | + config.addFilter("section", url=>url.replace(/^\/zh(?=\/)/,"").split("/")[1]||"home"); |
| 37 | + config.addFilter("withoutHeading", html=>html.replace(/<h1[^>]*>[\s\S]*?<\/h1>/,"")); |
| 38 | + config.addFilter("json", value=>JSON.stringify(value).replace(/</g,"\\u003c")); |
| 39 | + config.addTransform("deploymentPaths", function (content) { |
| 40 | + if (!(this.page?.outputPath || this.outputPath || "").endsWith(".html") || basePath === "/") return content; |
| 41 | + return content.replace(/(href|src|action)=("|')\/(?!\/)([^"']*)\2/g, (all,attr,quote,path)=>`${attr}=${quote}${path.startsWith(basePath.slice(1))?"/":""}${path.startsWith(basePath.slice(1))?path:basePath+path}${quote}`); |
| 42 | + }); |
| 43 | + return {dir:{input:"src",output:"_site"},markdownTemplateEngine:"njk",pathPrefix:basePath}; |
29 | 44 | } |
0 commit comments