Skip to content

Commit bf71cde

Browse files
committed
build: ensure Markdown nodes are not inserted when links array is empty
1 parent dea49e0 commit bf71cde

File tree

2 files changed

+55
-53
lines changed

2 files changed

+55
-53
lines changed

lib/node_modules/@stdlib/_tools/remark/plugins/remark-namespace-toc/lib/transformer.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,21 @@ function transformer( tree, file ) {
158158
if ( res.packages && res.packages.length > 0 ) {
159159
usedPackages = usedPackages.concat( res.packages );
160160
}
161-
newNode = {
162-
'type': 'html',
163-
'value': res.toc
164-
};
165-
debug( 'Index: %d', index );
166-
for ( i = index; i < parent.children.length; i++ ) {
167-
if ( parent.children[ i ].value === TOC_END ) {
168-
parent.children.splice( index+1, i-index-1, newNode );
169-
break;
170-
}
171-
}
172-
if ( i === parent.children.length - 1 ) {
173-
throw new Error( 'invalid node. Invalid table of contents (toc) comment. Ensure that the Markdown file includes both starting and ending `toc` comments. Node: `' + node.value + '`.' );
174-
}
175161
if ( res.links.length > 0 ) {
162+
newNode = {
163+
'type': 'html',
164+
'value': res.toc
165+
};
166+
debug( 'Index: %d', index );
167+
for ( i = index; i < parent.children.length; i++ ) {
168+
if ( parent.children[ i ].value === TOC_END ) {
169+
parent.children.splice( index+1, i-index-1, newNode );
170+
break;
171+
}
172+
}
173+
if ( i === parent.children.length - 1 ) {
174+
throw new Error( 'invalid node. Invalid table of contents (toc) comment. Ensure that the Markdown file includes both starting and ending `toc` comments. Node: `' + node.value + '`.' );
175+
}
176176
if ( found ) {
177177
debug( 'Append links to links section...' );
178178
newNode = {

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

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -188,48 +188,50 @@ function transformer( tree, file ) {
188188
if ( i === parent.children.length - 1 ) {
189189
throw new Error( format( 'invalid node. Ensure that the Markdown file includes both a starting `<section class="related">` and closing `</section>\n\n<!-- /.related -->`. Node: `%s`.', node.value ) );
190190
}
191-
if ( found ) {
192-
debug( 'Append links to links section...' );
193-
newNode = {
194-
'type': 'html',
195-
'value': links.join( '\n\n' )
196-
};
197-
for ( i = 0; i < parent.children.length; i++ ) {
198-
child = parent.children[ i ];
199-
if (
200-
child.type === 'html' &&
201-
child.value === '<!-- <related-links> -->'
202-
) {
203-
parent.children.splice( i+1, 0, newNode );
204-
break;
205-
}
206-
}
207-
} else {
208-
debug( 'Create <related-links> section...' );
209-
newNodes = [
210-
{
211-
'type': 'html',
212-
'value': '<!-- <related-links> -->'
213-
},
214-
{
191+
if ( links.length > 0 ) {
192+
if ( found ) {
193+
debug( 'Append links to links section...' );
194+
newNode = {
215195
'type': 'html',
216196
'value': links.join( '\n\n' )
217-
},
218-
{
219-
'type': 'html',
220-
'value': '<!-- </related-links> -->'
197+
};
198+
for ( i = 0; i < parent.children.length; i++ ) {
199+
child = parent.children[ i ];
200+
if (
201+
child.type === 'html' &&
202+
child.value === '<!-- <related-links> -->'
203+
) {
204+
parent.children.splice( i+1, 0, newNode );
205+
break;
206+
}
221207
}
222-
];
223-
for ( i = 0; i < parent.children.length; i++ ) {
224-
child = parent.children[ i ];
225-
if (
226-
child.type === 'html' &&
227-
child.value === '<!-- /.links -->'
228-
) {
229-
c = parent.children;
230-
c.splice.apply( c, [ i-1, 0 ].concat( newNodes ) );
231-
found = true;
232-
break;
208+
} else {
209+
debug( 'Create <related-links> section...' );
210+
newNodes = [
211+
{
212+
'type': 'html',
213+
'value': '<!-- <related-links> -->'
214+
},
215+
{
216+
'type': 'html',
217+
'value': links.join( '\n\n' )
218+
},
219+
{
220+
'type': 'html',
221+
'value': '<!-- </related-links> -->'
222+
}
223+
];
224+
for ( i = 0; i < parent.children.length; i++ ) {
225+
child = parent.children[ i ];
226+
if (
227+
child.type === 'html' &&
228+
child.value === '<!-- /.links -->'
229+
) {
230+
c = parent.children;
231+
c.splice.apply( c, [ i-1, 0 ].concat( newNodes ) );
232+
found = true;
233+
break;
234+
}
233235
}
234236
}
235237
}

0 commit comments

Comments
 (0)