Skip to content

build: add remark plugin for external file inclusions#11271

Open
batpigandme wants to merge 9 commits intostdlib-js:developfrom
batpigandme:remark-includes
Open

build: add remark plugin for external file inclusions#11271
batpigandme wants to merge 9 commits intostdlib-js:developfrom
batpigandme:remark-includes

Conversation

@batpigandme
Copy link
Copy Markdown
Member

@batpigandme batpigandme commented Apr 2, 2026

Description

What is the purpose of this pull request?

This pull request:

  • adds a new remark-includes plugin (@stdlib/_tools/remark/plugins/remark-includes) that resolves include directives in Markdown files, inserting content from external files between <!-- <include path="..."> --> and <!-- </include> --> comment tags
  • adds a standalone Makefile recipe (tools/make/lib/markdown/includes.mk) with markdown-includes and markdown-includes-files targets for running the plugin via make

Related Issues

Does this pull request have any related issues?

No.

Questions

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

Plugin behavior

  • Resolves include paths relative to the processed file's directory by default; configurable via a dir option
  • Warns and skips (via file.message()) on missing files rather than halting
  • Idempotent: re-running replaces existing content between tags
  • Uses the active remark processor context for parsing included content
  • No support for nested includes (included files are not recursively processed)

Future work: consider adding remark-includes to the to-html pipeline (@stdlib/_tools/markdown/to-html).

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

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

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
---
@batpigandme
Copy link
Copy Markdown
Member Author

batpigandme commented Apr 2, 2026

@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 .txt (as is done in other remark plugin fixtures) to avoid linting. I use the dir option in the README example at top, but the full snippet path works as well (see changes here for). We could require the full path for readability/findability's sake if we so choose. So, LMK, and I can ditch the dir option. Or we could have the default for dir go to a snippets folder if we want them all stored in one place.

My other two design questions are in the PR description (which I denoted with a little ❓ because humans can use emojis, too)!

@github-actions github-actions bot mentioned this pull request Apr 2, 2026
@batpigandme batpigandme marked this pull request as ready for review April 6, 2026 11:07
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Apr 6, 2026
@batpigandme batpigandme requested a review from kgryte April 6, 2026 11:07
@kgryte kgryte changed the title feat(remark-includes): add plugin for external file inclusions tools: add remark plugin for external file inclusions Apr 8, 2026
Comment thread lib/node_modules/@stdlib/_tools/remark/plugins/remark-includes/examples/index.js Outdated
Comment thread lib/node_modules/@stdlib/_tools/remark/plugins/remark-includes/lib/transformer.js Outdated
var end;
var i;

match = INCLUDE_START.exec( node.value );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check usage of exec here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check JS RegExp docs.

Copy link
Copy Markdown
Member Author

@batpigandme batpigandme Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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' ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

@batpigandme batpigandme Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread lib/node_modules/@stdlib/_tools/remark/plugins/remark-includes/README.md Outdated
Copy link
Copy Markdown
Member

@kgryte kgryte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good overall. Just a couple of minor nits.

@kgryte kgryte added the Tools Issue or pull request related to project tooling. label Apr 8, 2026
@kgryte kgryte changed the title tools: add remark plugin for external file inclusions build: add remark plugin for external file inclusions Apr 9, 2026
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
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Review A pull request which needs code review. Tools Issue or pull request related to project tooling.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants