Skip to content

The optimizer wrongly optimized the logic from marked #375

@GZTimeWalker

Description

@GZTimeWalker

Describe the bug

k = e.slice(0, s + r.index + f + a); has been optimized into o = e.slice(0, 0 + r.index + t + i); in emStrong function from marked, which caused a number of rendering misalignment issues.

source code: https://github.com/markedjs/marked/blob/5f4b638e09c30bc126be69e212c93b246c73ec54/src/Tokenizer.ts#L736

dev mode code:

emStrong(e, t, n = "") {
		let r = this.rules.inline.emStrongLDelim.exec(e);
		if (!r || r[3] && n.match(this.rules.other.unicodeAlphaNumeric)) return;
		if (!(r[1] || r[2] || "") || !n || this.rules.inline.punctuation.exec(n)) {
			let s = [...r[0]].length - 1, o, a, u = s, p = 0, c = r[0][0] === "*" ? this.rules.inline.emStrongRDelimAst : this.rules.inline.emStrongRDelimUnd;
			for (c.lastIndex = 0, t = t.slice(-1 * e.length + s); (r = c.exec(t)) != null;) {
				if (o = r[1] || r[2] || r[3] || r[4] || r[5] || r[6], !o) continue;
				if (a = [...o].length, r[3] || r[4]) {
					u += a;
					continue;
				} else if ((r[5] || r[6]) && s % 3 && !((s + a) % 3)) {
					p += a;
					continue;
				}
				if (u -= a, u > 0) continue;
				a = Math.min(a, a + u + p);
				let f = [...r[0]][0].length, k = e.slice(0, s + r.index + f + a);
				if (Math.min(s, a) % 2) {
					let g = k.slice(1, -1);
					return {
						type: "em",
						raw: k,
						text: g,
						tokens: this.lexer.inlineTokens(g)
					};
				}
				let x = k.slice(2, -2);
				return {
					type: "strong",
					raw: k,
					text: x,
					tokens: this.lexer.inlineTokens(x)
				};
			}
		}
	}

After build:

emStrong(e, t, n=``) {
        let r = this.rules.inline.emStrongLDelim.exec(e);
        if (!(!r || r[3] && n.match(this.rules.other.unicodeAlphaNumeric)) && (!(r[1] || r[2]) || !n || this.rules.inline.punctuation.exec(n))) {
            [...r[0]].length - 1;
            let n, i, a = 0, o = r[0][0] === `*` ? this.rules.inline.emStrongRDelimAst : this.rules.inline.emStrongRDelimUnd;
            for (o.lastIndex = 0,
            t = t.slice(-1 * e.length + 0); (r = o.exec(t)) != null; ) {
                if (n = r[1] || r[2] || r[3] || r[4] || r[5] || r[6],
                !n)
                    continue;
                if (i = [...n].length,
                r[3] || r[4]) {
                    a += i;
                    continue
                } else
                    r[5] || r[6];
                if (a -= i,
                a > 0)
                    continue;
                i = Math.min(i, i + a + 0);
                let t = [...r[0]][0].length
                  , o = e.slice(0, 0 + r.index + t + i);
                if (Math.min(0, i) % 2) {
                    let e = o.slice(1, -1);
                    return {
                        type: `em`,
                        raw: o,
                        text: e,
                        tokens: this.lexer.inlineTokens(e)
                    }
                }
                let s = o.slice(2, -2);
                return {
                    type: `strong`,
                    raw: o,
                    text: s,
                    tokens: this.lexer.inlineTokens(s)
                }
            }
        }
    }

Reproduction

not provide

Steps to reproduce

I don't know how to make a demo for this issue.

When use dev mode, everything works fine. But when pnpm build & pnpm preview, it won't work.

System Info

System:
    OS: macOS 15.6
    CPU: (10) arm64 Apple M1 Pro
    Memory: 1.91 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.5.0 - /opt/homebrew/bin/node
    npm: 11.5.1 - /opt/homebrew/bin/npm
    pnpm: 10.14.0 - /opt/homebrew/bin/pnpm
  Browsers:
    Safari: 18.6
  npmPackages:
    @vitejs/plugin-react: ^5.0.0 => 5.0.0 
    rolldown-vite:  7.1.2

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions