Skip to content

Migrate Core Patches to TypeScript#2853

Open
yihao03 wants to merge 6 commits intoMarkBind:masterfrom
yihao03:feat/migrate-core-patch
Open

Migrate Core Patches to TypeScript#2853
yihao03 wants to merge 6 commits intoMarkBind:masterfrom
yihao03:feat/migrate-core-patch

Conversation

@yihao03
Copy link
Contributor

@yihao03 yihao03 commented Mar 4, 2026

What is the purpose of this pull request?

  • Documentation update
  • Bug fix
  • Feature addition or enhancement
  • Code maintenance
  • DevOps
  • Improve developer experience
  • Others, please explain:

Overview of changes:
Migrated nunjucks, htmlparser2 and markdown-it patches to TypeScript and ESM.

Anything you'd like to highlight/discuss:
As these patches are modifying the internals of the npm packages, the types
we worked on are not exposed in the @types packages. We have to do module augmentation
to add the types that we are patching. Considering that the packages haven't been updated
in a while and will likely not be updated in the near future, we are fine with this approach.

Testing instructions:
Pull this branch and run npm run setup to make sure exisitng .js files are cleaned
and .ts files are compiled. Then run npm test to make sure all tests are passing.

Proposed commit message: (wrap lines at 72 characters)
Migrated nunjucks, htmlparser2 and markdown-it patches to TypeScript and ESM.


Checklist: ☑️

  • Updated the documentation for feature additions and enhancements
  • Added tests for bug fixes or features
  • Linked all related issues
  • No unrelated changes

Reviewer checklist:

Indicate the SEMVER impact of the PR:

  • Major (when you make incompatible API changes)
  • Minor (when you add functionality in a backward compatible manner)
  • Patch (when you make backward compatible bug fixes)

At the end of the review, please label the PR with the appropriate label: r.Major, r.Minor, r.Patch.

Breaking change release note preparation (if applicable):

  • To be included in the release note for any feature that is made obsolete/breaking

Give a brief explanation note about:

  • what was the old feature that was made obsolete
  • any replacement feature (if any), and
  • how the author should modify his website to migrate from the old feature to the replacement feature (if possible).

yihao03 added 4 commits March 4, 2026 15:05
created module augmentation in .d.ts files
for components patches, with types inferred by studying
the usage in the node_modules src files. added tsc compiled
.js files to .gitignore and prevent .d.ts files from being
ignored

Ignore compiled js and patched ts from linting
@codecov
Copy link

codecov bot commented Mar 4, 2026

Codecov Report

❌ Patch coverage is 76.14679% with 26 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.89%. Comparing base (5a90880) to head (ea62ea3).

Files with missing lines Patch % Lines
...re/src/patches/nunjucks/context-overrides-frame.ts 57.14% 24 Missing ⚠️
packages/core/src/patches/nunjucks/load-event.ts 81.81% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2853      +/-   ##
==========================================
- Coverage   71.92%   71.89%   -0.03%     
==========================================
  Files         132      132              
  Lines        7358     7359       +1     
  Branches     1516     1637     +121     
==========================================
- Hits         5292     5291       -1     
- Misses       2021     2062      +41     
+ Partials       45        6      -39     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR migrates MarkBind’s internal “core patches” (monkey-patches applied to nunjucks, htmlparser2, and a markdown-it custom-component patch set) from CommonJS .js files to TypeScript sources, and adds local .d.ts files to type internal/unstable APIs that aren’t covered by upstream @types/* packages.

Changes:

  • Converted patch entrypoints from *.js to *.ts and updated exports/imports accordingly.
  • Added manual TypeScript declaration files for patched/hidden internals (nunjucks submodules + internals, htmlparser2 internals).
  • Updated ignore lists to avoid committing compiled patch outputs while still tracking the “manual” declaration files.

Reviewed changes

Copilot reviewed 13 out of 17 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/core/src/patches/nunjucks/nunjucks-submodules.d.ts Adds ambient module declarations for untyped nunjucks internal submodules.
packages/core/src/patches/nunjucks/nunjucks-internals.d.ts Adds module augmentations for nunjucks internals used by MarkBind patches.
packages/core/src/patches/nunjucks/load-event.ts Migrates the nunjucks “load event” patch to TS + ES imports.
packages/core/src/patches/nunjucks/index.ts TS patch entrypoint (side-effect imports).
packages/core/src/patches/nunjucks/index.js Removes old CommonJS entrypoint.
packages/core/src/patches/nunjucks/context-overrides-frame.ts Migrates and types the MarkBind context/frame priority patch.
packages/core/src/patches/index.ts Migrates patch aggregator to TS and wires ignore-tag injections.
packages/core/src/patches/index.js Removes old CommonJS patch aggregator.
packages/core/src/patches/htmlparser2.ts Migrates htmlparser2 patch to TS + ES imports and exports.
packages/core/src/patches/htmlparser2-internal.d.ts Adds declaration augmentation for htmlparser2 internals used by the patch.
packages/core/src/lib/markdown-it/patches/custom-component/inlineTags.ts Converts export style to TS-friendly export =.
packages/core/src/lib/markdown-it/patches/custom-component/htmlRe.ts Converts to named ESM exports.
packages/core/src/lib/markdown-it/patches/custom-component/htmlInlineRule.ts Migrates to TS imports/exports and adds types.
packages/core/src/lib/markdown-it/patches/custom-component/htmlBlockRule.ts Migrates to TS imports/exports and adds types.
packages/core/src/lib/markdown-it/patches/custom-component/customComponentPlugin.ts Migrates plugin wiring to TS imports/exports and adds types.
.gitignore Ignores compiled patch JS outputs; attempts to unignore manual .d.ts files.
.eslintignore Ignores compiled patch JS outputs for linting.
Comments suppressed due to low confidence (3)

packages/core/src/patches/htmlparser2.ts:465

  • injectIgnoreTags is typed as string[], but the caller path (PluginManager -> patches.ignoreTags) passes a Set of tags. Since the implementation only spreads the input, it can support any iterable; consider changing the parameter type to Iterable<string> (or readonly string[]) and converting to an array before spreading if needed.
    packages/core/src/lib/markdown-it/patches/custom-component/customComponentPlugin.ts:41
  • injectTags is typed as tagsToIgnore: string[], but the upstream call path (PluginManager -> patches.ignoreTags) passes a Set of tags. Consider widening this (and the customComponentPlugin parameter) to Iterable<string> / ReadonlySet<string> | readonly string[] so the existing call-site type-checks without forcing an array conversion.
    packages/core/src/lib/markdown-it/patches/custom-component/htmlBlockRule.ts:6
  • initCustomComponentHtmlBlockRule is typed to take string[], but the surrounding logic (Array.from(tagsToIgnore)) works with any iterable and the upstream caller provides a Set of tags. Widening this parameter to Iterable<string> (or accepting Set<string> | string[]) would align with actual usage and avoid type errors once the caller is updated.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

import { injectTags } from '../lib/markdown-it/patches/custom-component/customComponentPlugin';
import { injectIgnoreTags } from './htmlparser2';

function ignoreTags(tagsToIgnore: string[]) {
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

ignoreTags is typed to accept string[], but its only caller (PluginManager._collectPluginTagConfigs) passes a Set of tag names. This will fail type-checking and also forces downstream helpers (injectIgnoreTags / injectTags) to accept only arrays even though they work fine with iterables. Consider widening the parameter type to Iterable<string> (or Set<string> | string[]) and updating the injected functions’ signatures accordingly.

Suggested change
function ignoreTags(tagsToIgnore: string[]) {
function ignoreTags(tagsToIgnore: Iterable<string>) {

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

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

Yes, the typing for this seems to be inaccurate?

Is it typecasting or something?

Copy link
Member

@gerteck gerteck left a comment

Choose a reason for hiding this comment

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

Highlighted some parts of the code that needs a further look and update, but generally looks good


const initCustomComponentHtmlBlockRule = require('./htmlBlockRule');
const htmlInlineRule = require('./htmlInlineRule');
import MarkdownIt from 'markdown-it';
Copy link
Member

Choose a reason for hiding this comment

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

Prefer using pure type imports

import type MarkdownIt from 'markdown-it';

@@ -1,13 +1,14 @@
const { HTML_TAG_RE } = require('./htmlRe');
import StateInline from 'markdown-it/lib/rules_inline/state_inline';
Copy link
Member

Choose a reason for hiding this comment

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

prefer using import type here as well

// Frame and contextOrFrameLookup. ESM namespace imports (import * as ...) are
// frozen objects, so we fall back to require() which returns the original mutable exports.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const globalRuntime: typeof runtimeTypes & Record<string, any> = require('nunjucks/src/runtime');
Copy link
Member

Choose a reason for hiding this comment

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

This seems like a future blocker:

This Nunjucks patch represents a critical but fragile architectural choice in MarkBind, specifically designed to override Nunjucks' default variable scoping so that variables passed via take priority over internal {% set %} declarations.

  • To achieve this, the code performs "monkey-patching" by manually overwriting the prototypes of internal, non-exported classes like Frame and Context. However, because modern TypeScript and ESM (EcmaScript Modules) treat imports as "frozen" read-only namespaces that cannot be modified, the implementation employs a "TypeScript hack" by using require('nunjucks/src/runtime') to obtain a mutable CommonJS object while using import type for development-time safety.
  • While this approach is functional in a CommonJS environment, it creates significant technical debt and follows a fragile pattern that relies on private library internals; shifting to a Pure ESM output in the future will break this mechanism entirely, as require will be unavailable and the ESM standard is explicitly designed to prevent this type of global prototype surgery.

Need to consider and think carefully how we should work on this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure on the part about require() being not available in typescript, see this. require can be available by importing from node:module, though it means that it wouldn’t work on browser runtime. however afaik the nunjucks code won’t be present in browser runtime?
alternatively, i have considered just cloning nunjucks to be part of this repo, considering its last release was 2 years ago meaning that it’s unlikely to get new features that we might need.

// ---------------------------------------------------------------------------
// Sub-module: nunjucks/src/runtime
// ---------------------------------------------------------------------------
declare module 'nunjucks/src/runtime' {
Copy link
Member

Choose a reason for hiding this comment

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

Duplicated nunjucks/src/runtime and nunjucks/src/object declarations in

  • internals.d.ts and
  • submodule.d.ts

Any reason why it was duplicated? Should it be removed from internals.d.ts?

Copy link
Member

Choose a reason for hiding this comment

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

  • nunjucks-submodules.d.ts exists solely to create the sub-modules (nunjucks/src/runtime, nunjucks/src/object) as ambient declarations.
  • nunjucks-internals.d.ts exists to augment the main nunjucks module, big declare module 'nunjucks' block at the bottom is its unique, non-duplicated work.

The sub-module blocks in it are dead weight that snuck in.
So the rule is: whoever creates something owns it. nunjucks-submodules.d.ts created the sub-modules, so it should keep the duplicated code. Delete the duplicate sub-module blocks from nunjucks-internals.d.ts.

import { injectTags } from '../lib/markdown-it/patches/custom-component/customComponentPlugin';
import { injectIgnoreTags } from './htmlparser2';

function ignoreTags(tagsToIgnore: string[]) {
Copy link
Member

Choose a reason for hiding this comment

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

Yes, the typing for this seems to be inaccurate?

Is it typecasting or something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants