comark icon indicating copy to clipboard operation
comark copied to clipboard

Subscript Markdown-it plugin no longer working as expected

Open will-stone opened this issue 2 months ago • 5 comments

Environment

  • Comark version: 0.7.0

Comark Version

0.7.0

Reproduction

import { parseMarkdown } from 'comark'
import { defineComarkPlugin } from 'comark/parse'
import markdownItSub from 'markdown-it-sub'

const subscript = defineComarkPlugin(() => ({
  markdownItPlugins: [markdownItSub],
  name: 'subscript',
}))

await parseMarkdown('a~footnote~', {
  plugins: [subscript()],
})

Description

This used to produce ["a", ["sub", {}, "footnote"]] (in the last release) but now produces a~footnote~.

Additional context

No response

Logs


will-stone avatar Sep 28 '26 10:09 will-stone

It looks like the state.pos and state.posMax has changed from (in my example above)

pos: 1,
posMax: 11,

to:

pos: 11,
posMax: 12,

This is causing the if (start + 2 >= max) { check to be true and for the plugin to exit early.

https://github.com/markdown-it/markdown-it-sub/blob/422e93885b3c611234d602aa795f3d75a62cc93e/index.mjs#L12

will-stone avatar Sep 28 '26 14:09 will-stone

I have solved this by writing my own Comark plugin:

import type { Node } from "comark"

function parseSubscript(node: Node): Node[] {
  if (typeof node === "string") {
    const nodes: Node[] = []

    for (const [index, part] of Object.entries(node.split(/~([^~\n]+)~/u))) {
      if (part) {
        const isEvenIndex = Number(index) % 2 === 0
        nodes.push(isEvenIndex ? part : ["sub", {}, part])
      }
    }

    return nodes
  }

  const [tag, props, ...children] = node

  // Comment node: [null, attrs, content] — don't split its body.
  if (tag === null) return [node]

  return [[tag, props ?? {}, ...children.flatMap(parseSubscript)]]
}

const subscript = defineComarkPlugin(() => ({
  name: "subscript",
  post(state) {
    state.tree.nodes = state.tree.nodes.flatMap(parseSubscript)
  },
}))

Please let me know if this looks okay, or maybe it could be turned into an official plugin along with superscript? 🤓

will-stone avatar Sep 28 '26 16:09 will-stone

Thanks for the plugin! While looking into this I traced the root cause, in case it helps:

Since 0.7.0 (#394), the auto-close pre-pass escapes unmatched single tildes and only exempts the word~digits~word pattern (H~2~O). So a~footnote~ reaches markdown-it as a\~footnote~, and markdown-it-sub never sees a pair. That means existing markdown-it sub/sup plugins break too, not only for this use case.

Two possible directions, depending on what the maintainers prefer:

  1. Widen the exemption: also leave word~text~ alone when there's no whitespace inside and the closing ~ isn't followed by a word character (a~note~ , x~ab~.).
  2. Skip single-tilde escaping when a subscript plugin is registered, and keep the current behavior otherwise.

I have a small experiment for (1) with regression tests against the real markdown-it-sub/markdown-it-sup, if it's useful as a reference.

0froq avatar Oct 17 '26 20:10 0froq

Can I work on this?

aliassar avatar Nov 03 '26 15:11 aliassar

Thanks for the report, Contribution always is more than welcome, feel free to create a PR and small set of tests to prevent regression is future :)

farnabaz avatar Nov 05 '26 12:11 farnabaz