Skip to content

Commit f341d2e

Browse files
authored
fix: Only highlight first term in paragraph [INGEST-1262] (#5346)
1 parent 61d570f commit f341d2e

File tree

1 file changed

+37
-24
lines changed
  • src/gatsby/plugins/gatsby-remark-terms

1 file changed

+37
-24
lines changed

src/gatsby/plugins/gatsby-remark-terms/index.js

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,30 @@ const REGEX = new RegExp(`(\\b|\\W)(${PATTERN})(\\b|\\W)`);
2424
const CUSTOM_LINK_START = new RegExp("^<([a-zA-Z]+Link|a) ");
2525
const CUSTOM_LINK_END = new RegExp("^</([a-zA-Z]+Link|a)>");
2626

27-
function replace(node) {
27+
function replace(state, node) {
28+
if (node.type == "root") return;
29+
2830
// If this is an empty node there's nothing to consider.
29-
if (!node.children) return;
31+
if (!node.children) return "skip";
3032

3133
// Do not replace abbreviations in headings because that appears to break the heading anchors.
32-
if (node.type == "heading") return;
34+
if (node.type == "heading") {
35+
state.alreadyExplained = {};
36+
return "skip";
37+
}
3338

3439
// Do not replace abbreviations in links because that's two interactive
3540
// nested elements nested in each other, and we decided we don't want to
3641
// style that either.
3742
//
3843
// This currently doesn't handle nesting of e.g.
3944
// <a><strong><abbr>... but we don't have that in docs.
40-
if (node.type == "link") return;
45+
if (node.type == "link") return "skip";
4146

4247
let insideCustomLink = false;
4348

49+
const newChildren = [];
50+
4451
// If a text node is present in child nodes, check if an abbreviation is present
4552
for (let c = 0; c < node.children.length; c++) {
4653
const child = node.children[c];
@@ -85,36 +92,42 @@ function replace(node) {
8592
}
8693
}
8794

88-
if (insideCustomLink) continue;
89-
if (child.type !== "text") continue;
90-
if (!REGEX.test(child.value)) continue;
95+
if (insideCustomLink || child.type !== "text" || !REGEX.test(child.value)) {
96+
newChildren.push(child);
97+
continue;
98+
}
9199

92100
// Transform node
93101
const newTexts = child.value.split(REGEX);
94102

95-
// Remove old text node
96-
node.children.splice(c, 1);
97-
98103
// Replace abbreviations
99104
for (let i = 0; i < newTexts.length; i++) {
100105
const content = newTexts[i];
101-
node.children.splice(
102-
c + i,
103-
0,
104-
TERMS[content]
105-
? {
106-
type: "html",
107-
value: `<div class="term-wrapper"><span class="term">${content}</span><span class="description" role="tooltip" aria-label="${content} definition">${TERMS[content]}</span></div>`,
108-
}
109-
: {
110-
type: "text",
111-
value: content,
112-
}
113-
);
106+
let newNode;
107+
if (TERMS[content] && !state.alreadyExplained[content]) {
108+
state.alreadyExplained[content] = true;
109+
newNode = {
110+
type: "html",
111+
value: `<div class="term-wrapper"><span class="term">${content}</span><span class="description" role="tooltip" aria-label="${content} definition">${TERMS[content]}</span></div>`,
112+
};
113+
} else {
114+
newNode = {
115+
type: "text",
116+
value: content,
117+
};
118+
}
119+
120+
newChildren.push(newNode);
114121
}
115122
}
123+
124+
node.children = newChildren;
116125
}
117126

118127
module.exports = async ({ markdownAST }) => {
119-
visit(markdownAST, () => true, replace);
128+
const state = {
129+
alreadyExplained: {},
130+
};
131+
const replaceWithState = replace.bind(null, state);
132+
visit(markdownAST, () => true, replaceWithState);
120133
};

0 commit comments

Comments
 (0)