Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ const config = {
onBrokenMarkdownImages: "throw",
},
mermaid: true,
// Interpolate ||site.*|| tokens on raw file content, for every md/mdx file.
// The remark-replace plugin below only runs on docs pages; partials imported
// from _includes/ are compiled by Docusaurus' MDX *fallback* loader, which
// does not receive the docs preset's remarkPlugins, so ||site.*|| tokens in
// those partials (e.g. code snippets in the quickstart install tabs) were
// never interpolated. This preprocessor covers every file, including
// fallback-loaded partials. It shares one implementation with the plugin and
// is idempotent, so it does not affect pages the plugin already handles.
preprocessor: ({ fileContent, filePath }) =>
remarkReplace.replaceSiteTokens(fileContent, filePath),
},

i18n: {
Expand Down
14 changes: 12 additions & 2 deletions src/remark/remark-replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@ const siteConfig = readSiteConfig();
// pattern to match: ||site.some_name|| in markdown
const pattern = /[|]{2}[ ]*site\.([a-z_]*)[ ]*[|]{2}/g

const interpolate = (value, vfile) => {
// Shared token replacement. Used by the remark plugin (below) for docs pages,
// and by the site's markdown.preprocessor (docusaurus.config.js) so that the
// same ||site.*|| interpolation also runs on imported partials from _includes/,
// which are compiled by Docusaurus' MDX *fallback* loader and therefore never
// see this remark plugin. Keeping one implementation avoids drift.
const replaceSiteTokens = (value, sourcePath) => {
// replace the pattern with config variables
return value.replaceAll(pattern, (_, name) => {
// display a warning if config variable is missing
if (siteConfig[name] == undefined) {
console.warn('\x1b[33m%s', vfile.path, '\x1b[0m');
console.warn('\x1b[33m%s', sourcePath, '\x1b[0m');
console.warn(`Couldn't find`, '\x1b[31m', `||site.${name}||`, '\x1b[0m');
}
return siteConfig[name]
})
}

const interpolate = (value, vfile) => replaceSiteTokens(value, vfile.path)

const valueNodes = ['text', 'jsx', 'code', 'inlineCode'];
const valueNodeFilter = (node) =>
valueNodes.includes(node.type) && // contains one of the types in valueNodes
Expand All @@ -51,3 +58,6 @@ const plugin = (options) => {
};

module.exports = plugin;
// Exposed so docusaurus.config.js `markdown.preprocessor` can reuse the exact
// same interpolation on raw file content (covers fallback-loaded partials).
module.exports.replaceSiteTokens = replaceSiteTokens;
Loading