microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
docs/.eleventy.js
190lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | const markdownItReplaceLink = require("markdown-it-replace-link"); |
| 5 | |
| 6 | module.exports = function (eleventyConfig) { |
| 7 | const pathPrefix = process.env.GITHUB_REPOSITORY |
| 8 | ? process.env.GITHUB_REPOSITORY.split("/")[1] |
| 9 | : "TypeAgent"; |
| 10 | |
| 11 | // Copy static assets |
| 12 | eleventyConfig.addPassthroughCopy("_includes"); |
| 13 | eleventyConfig.addPassthroughCopy("assets"); |
| 14 | eleventyConfig.addPassthroughCopy("content/imgs"); |
| 15 | eleventyConfig.addPassthroughCopy("content/tutorial/imgs"); |
| 16 | |
| 17 | eleventyConfig.addShortcode("version", function () { |
| 18 | return String(Date.now()); |
| 19 | }); |
| 20 | |
| 21 | // Add a shortcode for the current year |
| 22 | eleventyConfig.addShortcode("year", function () { |
| 23 | return new Date().getFullYear(); |
| 24 | }); |
| 25 | |
| 26 | let markdownIt = require("markdown-it"); |
| 27 | let markdownItAnchor = require("markdown-it-anchor"); |
| 28 | let options = { |
| 29 | html: true, |
| 30 | breaks: true, |
| 31 | linkify: true, |
| 32 | }; |
| 33 | |
| 34 | // Add a shortcode for repository links |
| 35 | eleventyConfig.addShortcode("repo", function (path, text) { |
| 36 | const repoUrl = process.env.GITHUB_REPOSITORY |
| 37 | ? `https://github.com/${process.env.GITHUB_REPOSITORY}` |
| 38 | : this.ctx?.site?.github || "https://github.com/microsoft/TypeAgent"; |
| 39 | |
| 40 | const defaultBranch = process.env.GITHUB_DEFAULT_BRANCH || "main"; |
| 41 | |
| 42 | const normalizedPath = path.replace(/^\.\.\/|^\//g, ""); |
| 43 | |
| 44 | // Determine the correct GitHub URL based on whether it's a file or directory |
| 45 | const isDirectory = !normalizedPath.includes("."); |
| 46 | const githubUrl = isDirectory |
| 47 | ? `${repoUrl}/tree/${defaultBranch}/${normalizedPath}` |
| 48 | : `${repoUrl}/blob/${defaultBranch}/${normalizedPath}`; |
| 49 | |
| 50 | // Return markdown link |
| 51 | return `[${text || normalizedPath}](${githubUrl})`; |
| 52 | }); |
| 53 | |
| 54 | // Add debugging shortcode to show the current URL |
| 55 | eleventyConfig.addShortcode("debugUrl", function () { |
| 56 | return ` |
| 57 | <div style="background: #f8d7da; padding: 10px; margin: 10px 0; border: 1px solid #f5c6cb;"> |
| 58 | <p><strong>Debug Path Information:</strong></p> |
| 59 | <p>Path Prefix: ${pathPrefix}</p> |
| 60 | <p>Full Base URL: ${this.page ? this.page.url : "No page context"}</p> |
| 61 | </div> |
| 62 | `; |
| 63 | }); |
| 64 | |
| 65 | // Set up markdown-it with the plugins |
| 66 | eleventyConfig.setLibrary( |
| 67 | "md", |
| 68 | markdownIt(options) |
| 69 | .use(markdownItAnchor, { |
| 70 | permalink: true, |
| 71 | permalinkClass: "direct-link", |
| 72 | permalinkSymbol: "#", |
| 73 | }) |
| 74 | .use(markdownItReplaceLink, { |
| 75 | replaceLink: function (link, env) { |
| 76 | // Only process relative image links that don't start with "/" |
| 77 | if ( |
| 78 | link && |
| 79 | !link.startsWith("/") && |
| 80 | !link.startsWith("http") && |
| 81 | !link.startsWith("#") && |
| 82 | /\.(jpeg|jpg|gif|png|svg)$/.test(link) |
| 83 | ) { |
| 84 | // Get the file path from the environment |
| 85 | const inputPath = env.page.inputPath; |
| 86 | |
| 87 | // Extract directory from the input path |
| 88 | const dir = inputPath.substring(0, inputPath.lastIndexOf("/")); |
| 89 | |
| 90 | // For images in the same directory as the markdown file, |
| 91 | // construct a path relative to the site root |
| 92 | |
| 93 | if (dir.includes("/tutorial") && link.startsWith("imgs/")) { |
| 94 | return `/TypeAgent/content/tutorial/imgs/${link.substring(5)}`; |
| 95 | } |
| 96 | |
| 97 | if (dir.includes("/content")) { |
| 98 | const index = link.indexOf("imgs/"); |
| 99 | return `/TypeAgent/content/imgs/${link.substring(index + 5)}`; |
| 100 | } |
| 101 | |
| 102 | return link; |
| 103 | } |
| 104 | return link; |
| 105 | }, |
| 106 | }) |
| 107 | ); |
| 108 | |
| 109 | // Create a collection for documentation pages |
| 110 | eleventyConfig.addCollection("docs", function (collection) { |
| 111 | return collection |
| 112 | .getFilteredByGlob("content/**/*.md") |
| 113 | .filter((item) => !item.filePathStem.includes("index")); |
| 114 | }); |
| 115 | |
| 116 | // Store a map of input paths to output paths |
| 117 | const pageMap = new Map(); |
| 118 | eleventyConfig.addTransform("recordPaths", function (content, outputPath) { |
| 119 | const inputPath = this.inputPath; |
| 120 | |
| 121 | if (inputPath && outputPath) { |
| 122 | pageMap.set(outputPath, inputPath); |
| 123 | } |
| 124 | |
| 125 | return content; |
| 126 | }); |
| 127 | |
| 128 | const { updateLinks } = require("./scripts/update-links"); |
| 129 | |
| 130 | eleventyConfig.addTransform("updateLinks", function (content, outputPath) { |
| 131 | if (!outputPath || !outputPath.endsWith(".html")) { |
| 132 | return content; // Only process HTML files |
| 133 | } |
| 134 | |
| 135 | // Get the original input path for this file |
| 136 | const inputPath = pageMap.get(outputPath) || this.inputPath; |
| 137 | |
| 138 | if (!inputPath) { |
| 139 | console.warn( |
| 140 | `No input path found for ${outputPath}, skipping link transformation` |
| 141 | ); |
| 142 | return content; |
| 143 | } |
| 144 | |
| 145 | const repoUrl = process.env.GITHUB_REPOSITORY |
| 146 | ? `https://github.com/${process.env.GITHUB_REPOSITORY}` |
| 147 | : this.ctx?.site?.github || "https://github.com/microsoft/TypeAgent"; |
| 148 | |
| 149 | const defaultBranch = process.env.GITHUB_DEFAULT_BRANCH || "main"; |
| 150 | |
| 151 | console.log(`Transforming links in: ${outputPath} (from ${inputPath})`); |
| 152 | return updateLinks(content, outputPath, inputPath, repoUrl, defaultBranch); |
| 153 | }); |
| 154 | |
| 155 | // Add a filter for GitHub repository URLs |
| 156 | eleventyConfig.addFilter("githubUrl", function (path, isDirectory = false) { |
| 157 | const repoUrl = process.env.GITHUB_REPOSITORY |
| 158 | ? `https://github.com/${process.env.GITHUB_REPOSITORY}` |
| 159 | : this.ctx?.site?.github || "https://github.com/microsoft/TypeAgent"; |
| 160 | |
| 161 | const defaultBranch = process.env.GITHUB_DEFAULT_BRANCH || "main"; |
| 162 | |
| 163 | const normalizedPath = path.replace(/^\.\.\/|^\//g, ""); |
| 164 | |
| 165 | return isDirectory |
| 166 | ? `${repoUrl}/tree/${defaultBranch}/${normalizedPath}` |
| 167 | : `${repoUrl}/blob/${defaultBranch}/${normalizedPath}`; |
| 168 | }); |
| 169 | |
| 170 | return { |
| 171 | dir: { |
| 172 | // Input directory is the current directory (docs folder) |
| 173 | input: ".", |
| 174 | // Output to a _site subdirectory within docs |
| 175 | output: "_site", |
| 176 | // Layouts are in _includes |
| 177 | includes: "_includes", |
| 178 | // Data files are in _data |
| 179 | data: "_data", |
| 180 | // Content files are in the content directory |
| 181 | layouts: "_includes", |
| 182 | }, |
| 183 | templateFormats: ["md", "njk", "html"], |
| 184 | markdownTemplateEngine: "njk", |
| 185 | htmlTemplateEngine: "njk", |
| 186 | dataTemplateEngine: "njk", |
| 187 | passthroughFileCopy: true, |
| 188 | pathPrefix: `/${pathPrefix}/`, |
| 189 | }; |
| 190 | }; |
| 191 | |