Skip to content

Commit 2f17312

Browse files
committed
fix(remark-includes): address review feedback
Create a local remark parser in the transformer and use it to parse included markdown content. This removes reliance on processor context being forwarded through the attacher, which improves robustness. Update docs and examples to use the fs/read-file sync helper and document that include blocks must be non-overlapping sibling regions. Assisted-by: Claude-Sonnet-4-6 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 88912e5 commit 2f17312

4 files changed

Lines changed: 12 additions & 12 deletions

File tree

lib/node_modules/@stdlib/_tools/remark/plugins/remark-includes/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ and inserts the parsed Markdown content of the referenced file between the openi
6767

6868
The opening include directive requires a `path` attribute with a double-quoted value.
6969

70+
Include blocks must be non-overlapping sibling regions. Nested or overlapping include blocks are not supported.
71+
7072
The plugin accepts the following `options`:
7173

7274
- **dir**: base directory for resolving include paths. Default: `null` (resolved relative to the processed file's directory).
@@ -87,8 +89,8 @@ If an opening include directive does not have a matching closing include directi
8789

8890
```javascript
8991
var join = require( 'path' ).join;
90-
var readFileSync = require( 'fs' ).readFileSync;
9192
var remark = require( 'remark' );
93+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
9294
var includes = require( '@stdlib/_tools/remark/plugins/remark-includes' );
9395

9496
// Load a Markdown file...

lib/node_modules/@stdlib/_tools/remark/plugins/remark-includes/examples/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
'use strict';
2020

21-
var readFileSync = require( 'fs' ).readFileSync;
2221
var join = require( 'path' ).join;
2322
var remark = require( 'remark' );
23+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
2424
var includes = require( './../lib' );
2525

2626
// Load a Markdown file...

lib/node_modules/@stdlib/_tools/remark/plugins/remark-includes/lib/attacher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function attacher( options ) {
6262
}
6363
}
6464
debug( 'Attaching a plugin configured with the following options: %s', JSON.stringify( opts ) );
65-
return transformerFactory( opts, this ); // eslint-disable-line no-invalid-this
65+
return transformerFactory( opts );
6666
}
6767

6868

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// MODULES //
2222

2323
var resolve = require( 'path' ).resolve;
24+
var remark = require( 'remark' );
2425
var logger = require( 'debug' );
2526
var visit = require( 'unist-util-visit' );
2627
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
@@ -42,10 +43,12 @@ var INCLUDE_END = /<!--\s*<\/include>\s*-->/;
4243
* @private
4344
* @param {Options} opts - transformer options
4445
* @param {(string|null)} opts.dir - base directory for resolving include paths
45-
* @param {Object} processor - remark processor instance
4646
* @returns {Function} transformer function
4747
*/
48-
function factory( opts, processor ) {
48+
function factory( opts ) {
49+
var parser;
50+
51+
parser = remark();
4952
return transformer;
5053

5154
/**
@@ -127,19 +130,14 @@ function factory( opts, processor ) {
127130
debug( 'Found closing include comment at index: %d', end );
128131

129132
// Parse the included Markdown content into AST nodes:
130-
if ( processor && typeof processor.parse === 'function' ) {
131-
children = processor.parse( content ).children;
132-
} else {
133-
file.message( format( 'unable to parse include using processor context: %s', fpath ), node );
134-
return;
135-
}
133+
children = parser.parse( content ).children;
136134
debug( 'Parsed %d nodes from include file.', children.length );
137135

138136
// Remove existing nodes between the opening and closing tags:
139137
parent.children.splice( index + 1, end - index - 1 );
140138

141139
// Insert the parsed nodes between the opening and closing tags:
142-
Array.prototype.splice.apply(parent.children, [ index + 1, 0 ].concat( children ));
140+
Array.prototype.splice.apply( parent.children, [ index + 1, 0 ].concat( children ) ); // eslint-disable-line max-len
143141
debug( 'Inserted include content.' );
144142
}
145143
}

0 commit comments

Comments
 (0)