Skip to content

Commit 98eee8f

Browse files
committed
fix: accommodate math code blocks within Markdown equations
1 parent aefab30 commit 98eee8f

File tree

1 file changed

+52
-2
lines changed
  • lib/node_modules/@stdlib/_tools/remark/plugins/remark-svg-equations/lib

1 file changed

+52
-2
lines changed

lib/node_modules/@stdlib/_tools/remark/plugins/remark-svg-equations/lib/transformer.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,66 @@ function transformer( tree, file, clbk ) {
149149
parent = equations[ idx ].parent;
150150
i = equations[ idx ].index;
151151

152-
// Case 1: insert new node between equation tags...
152+
/*
153+
* Case 1: insert new node between equation tags.
154+
*
155+
* ```markdown
156+
* <!-- <equation class="equation" label="eq:triangular_root" align="center" raw="n = \frac{\sqrt{8x+1} - 1}{2}" alt="Triangular root formula."> -->
157+
*
158+
* <!-- </equation> -->
159+
* ```
160+
*/
153161
if ( EQN_END.test( parent.children[ i+1 ].value ) ) {
154162
debug( 'Inserting new node...' );
155163
parent.children.splice( i+1, 0, newNode );
156164
}
157-
// Case 2: replace existing node...
165+
/*
166+
* Case 2: replace existing node.
167+
*
168+
* ```markdown
169+
* <!-- <equation class="equation" label="eq:triangular_root" align="center" raw="n = \frac{\sqrt{8x+1} - 1}{2}" alt="Triangular root formula."> -->
170+
*
171+
* <div class="equation" align="center" data-raw-text="n = \frac{\sqrt{8x+1} - 1}{2}" data-equation="eq:triangular_root">
172+
* <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@b295a09a80f4fd0cc84682dcda0fe3e354394c0c/lib/node_modules/@stdlib/assert/is-square-triangular-number/docs/img/equation_triangular_root.svg" alt="Triangular root formula.">
173+
* <br>
174+
* </div>
175+
*
176+
* <!-- </equation> -->
177+
* ```
178+
*/
158179
else if ( EQN_END.test( parent.children[ i+2 ].value ) ) {
159180
debug( 'Replacing existing node...' );
160181
parent.children[ i+1 ] = newNode;
161182
}
183+
/*
184+
* Case 3: replace existing nodes.
185+
*
186+
* ```markdown
187+
* <!-- <equation class="equation" label="eq:triangular_root" align="center" raw="n = \frac{\sqrt{8x+1} - 1}{2}" alt="Triangular root formula."> -->
188+
*
189+
* \`\`\`math
190+
* n = \frac{\sqrt{8x+1} - 1}{2}
191+
* \`\`\`
192+
*
193+
* <!-- <div class="equation" align="center" data-raw-text="n = \frac{\sqrt{8x+1} - 1}{2}" data-equation="eq:triangular_root">
194+
* <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@b295a09a80f4fd0cc84682dcda0fe3e354394c0c/lib/node_modules/@stdlib/assert/is-square-triangular-number/docs/img/equation_triangular_root.svg" alt="Triangular root formula.">
195+
* <br>
196+
* </div> -->
197+
*
198+
* <!-- </equation> -->
199+
* ```
200+
*/
201+
else if ( EQN_END.test( parent.children[ i+3 ].value ) ) {
202+
debug( 'Replacing existing nodes...' );
203+
204+
// Note: we don't splice--we simply replace--in order to avoid invalidating the indices of the equation elements in the AST. If we were to remove children, a subsequent resolved equation index would no longer be accurate...
205+
parent.children[ i+1 ] = newNode;
206+
parent.children[ i+1 ] = {
207+
'type': 'html',
208+
'value': '<!-- -->'
209+
};
210+
}
211+
// Otherwise, an invalid node...
162212
else {
163213
debug( 'Invalid node: %s', parent.children[ idx ].value );
164214
error = new Error( format( 'invalid node. Invalid equation comment. Ensure that the Markdown file includes both starting and ending equation comments. Node: `%s`.', parent.children[ idx ].value ) );

0 commit comments

Comments
 (0)