build: add remark plugin for external file inclusions#11271
build: add remark plugin for external file inclusions#11271batpigandme wants to merge 9 commits intostdlib-js:developfrom
Conversation
Add a remark plugin to insert content from external Markdown files into a document using HTML comment directives. The plugin resolves include paths relative to the file being processed or a specified base directory, parses the external content into AST nodes, and replaces the content between opening and closing tags.
Assisted-by: Claude-Opus-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: passed
- 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
---
Add `markdown-includes` and `markdown-includes-files` rules to process Markdown files using the `remark-includes` plugin, enabling the automated insertion of content from external files.
---
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: na
- task: lint_package_json
status: na
- task: lint_repl_help
status: na
- task: lint_javascript_src
status: na
- task: lint_javascript_cli
status: na
- task: lint_javascript_examples
status: na
- 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
---
Add a check to verify that a base directory is available before attempting to resolve an include path and report an error if resolution is not possible. Assisted-by: Codex-GPT-5-4
Stores the current processor via this and parses include content with processor.parse, keeping parsing behavior aligned with the main remark pipeline. Assisted-by: GPT-5-4
Capture the remark processor context in the attacher via `this` and
pass it to the transformer factory, avoiding `consistent-this` and
`no-invalid-this` violations. Flip negated condition to satisfy
`no-negated-condition` and wrap long line for `max-len` compliance.
Assisted-by: Claude Opus 4.6 <noreply@anthropic.com>
---
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: na
- 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: na
- 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
---
---
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: na
- task: lint_javascript_cli
status: na
- task: lint_javascript_examples
status: na
- 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
---
Update the README to clarify how include paths are resolved when processing in-memory strings, consolidate usage examples, and document directive requirements and error handling behavior.
Assisted-by: GPT-5-3
---
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: na
- task: lint_javascript_cli
status: na
- task: lint_javascript_examples
status: na
- 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
---
|
@kgryte, I've set this up so that the example use case we discussed wherein: # Hello World!
<!--<include path="./docs/snippets/foo.md"> -->
<!--</include> -->
<!-- <include path="./docs/snippets/bar.md"> -->
<!-- </include> -->
Would render to # Hello World!
<!--<include path="./docs/snippets/foo.md"> -->
## Greeting
Beep boop.
<!--</include> -->
<!--<include path="./docs/snippets/bar.md"> -->
## Bap
Bip bop.
<!--</include> -->
The fixture examples use My other two design questions are in the PR description (which I denoted with a little ❓ because humans can use emojis, too)! |
| var end; | ||
| var i; | ||
|
|
||
| match = INCLUDE_START.exec( node.value ); |
There was a problem hiding this comment.
Works because code does not rely on regex state across the document (each HTML node is visited independently with INCLUDE_START.exec(node.value) being run by transformer.js only against that one node, and then moves on), and this line is effectively being used as a parser for one HTML node (not an interator over many matches).
Only limitation is for nested includes, and I've added a note about that in the README.
| debug( 'Found closing include comment at index: %d', end ); | ||
|
|
||
| // Parse the included Markdown content into AST nodes: | ||
| if ( processor && typeof processor.parse === 'function' ) { |
There was a problem hiding this comment.
In what context does the attacher pass down undefined for the this argument? Meaning, is this check necessary? May be worthwhile to check our other remark plugins to see if we have similar logic or not.
There was a problem hiding this comment.
It is not necessary. Refactored to avoid this and match the patterns used in the other remark plugins. Only tradeoff is that it is now parsed with a standalone remark instance, so won't inherit parser extensions configured on outer processor. So, it's stricter consistency with sibling plugins, but, if we end up wanting outer parser extensions to be inherited, the this approach in attacher.js and using the (now removed) guard would be appropriate (i.e. if we want extensions configured on the outer processor—remark-gfm, math directives, etc.—they now won't apply to included files).
kgryte
left a comment
There was a problem hiding this comment.
Looking good overall. Just a couple of minor nits.
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
---
Alias `parent.children` as `c` immediately before the `splice.apply`
call and invoke through `c.splice.apply( c, ... )`.
Assisted-by: Claude Opus 4.6 <noreply@anthropic.com>
---
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: na
- 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: na
- 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
---
Description
This pull request:
remark-includesplugin (@stdlib/_tools/remark/plugins/remark-includes) that resolves include directives in Markdown files, inserting content from external files between<!-- <include path="..."> -->and<!-- </include> -->comment tagstools/make/lib/markdown/includes.mk) withmarkdown-includesandmarkdown-includes-filestargets for running the plugin viamakeRelated Issues
No.
Questions
No.
Other
Plugin behavior
diroptionfile.message()) on missing files rather than haltingFuture work: consider adding
remark-includesto theto-htmlpipeline (@stdlib/_tools/markdown/to-html).Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
This PR was written primarily by Claude Code, with manual review and iteration on lint fixes, parser context handling, and base-path guards.
@stdlib-js/reviewers