Skip to content

Commit

Permalink
Add JSX nodes to variable injector
Browse files Browse the repository at this point in the history
  • Loading branch information
TucksonDev committed Sep 4, 2023
1 parent 8152151 commit fb63e12
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions website/src/remark/variable-injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,32 @@

const visit = require('unist-util-visit');

// Allowed types (in alphabetical order)
const allowedTypes = ['code', 'definition', 'inlineCode', 'jsx', 'link', 'text'];

const plugin = (options) => {
return async (ast) => {
// visit() will match all nodes form the AST that have one of the types specified
// in the second argument.
// In those nodes, we want to inject variables in different fields:
// - For 'text' 'code' and 'inlineCode' nodes, we'll look in the "value" property
// - For 'code', 'inlineCode', 'jsx' and 'text' nodes, we'll look in the "value" property
// - For 'link' and 'definition' nodes, we'll look in the "url" property
// Nodes generated in AST are referenced here: https://github.com/syntax-tree/mdast
// Note: to "visit" a node, reference it here in camelCase
visit(ast, ['text', 'code', 'inlineCode', 'link', 'definition'], (node) => {
visit(ast, allowedTypes, (node) => {
let value;
switch (node.type) {
case 'link':
case 'definition':
value = node.url;
break;

case 'text':
case 'code':
case 'inlineCode':
case 'jsx':
case 'text':
value = node.value;
break;

case 'definition':
case 'link':
value = node.url;
break;
}

// This matches text between two '@'.
Expand All @@ -51,16 +55,17 @@ const plugin = (options) => {
});

switch (node.type) {
case 'link':
case 'definition':
node.url = value;
break;

case 'text':
case 'code':
case 'inlineCode':
case 'jsx':
case 'text':
node.value = value;
break;

case 'definition':
case 'link':
node.url = value;
break;
}
});
};
Expand Down

0 comments on commit fb63e12

Please sign in to comment.