Skip to content

Commit

Permalink
Fix image links on front page
Browse files Browse the repository at this point in the history
  • Loading branch information
emlai committed Jul 24, 2022
1 parent 9f30df7 commit b79e24f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const lightCodeTheme = require('prism-react-renderer/themes/github');
const darkCodeTheme = require('prism-react-renderer/themes/dracula');
const transformLinks = require('./src/plugins/transformLinks');
const transformImageLinks = require('./src/plugins/transformImageLinks');

/** @type {import('@docusaurus/types').Config} */
const config = {
Expand Down Expand Up @@ -37,6 +38,7 @@ const config = {
sidebarPath: require.resolve('./sidebars.js'),
editUrl: 'https://github.com/carbon-language/carbon-lang/blob/trunk/docs',
beforeDefaultRemarkPlugins: [transformLinks],
rehypePlugins: [transformImageLinks],
},
theme: {
customCss: require.resolve('./src/css/custom.css'),
Expand Down
17 changes: 17 additions & 0 deletions website/src/plugins/transformImageLinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const visit = require('unist-util-visit');

// Adjust HTML image links referring inside the repo to work on the website.
const plugin = (options) => {
const transformer = (ast) => {
visit(ast, 'jsx', (node) => {
// if (node.value.includes('src="docs'))
// console.log(node);
node.value = node.value
.replaceAll(/\bhref="(docs\/[^"]+)"/g, 'href="https://github.com/carbon-language/carbon-lang/blob/trunk/$1"')
.replaceAll(/\bsrc="(docs\/[^"]+)"/g, 'src="https://raw.githubusercontent.com/carbon-language/carbon-lang/trunk/$1"');
});
};
return transformer;
};

module.exports = plugin;

0 comments on commit b79e24f

Please sign in to comment.