Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block level extension not rendering correctly #3272

Closed
arfianadam opened this issue Apr 29, 2024 · 2 comments
Closed

Block level extension not rendering correctly #3272

arfianadam opened this issue Apr 29, 2024 · 2 comments
Labels

Comments

@arfianadam
Copy link

Marked version: 12.0.2

I tried to write an extension that also stores image's width, but despite following the example, I couldn't make it to output what I expected.

To Reproduce

Codesandbox.io

marked.use({
  extensions: [
    {
      name: "customImage",
      level: "block",
      start: (source: string) => {
        return source.match(/!\[/)?.index;
      },
      tokenizer(source, tokens) {
        const rule = /!\[\]\((.+?)\){: width=(\d+?) }/;
        const match = rule.exec(source);
        if (match) {
          const token = {
            type: "customImage",
            raw: match[0],
            src: match[1],
            width: match[2],
          };
          return token;
        }
        return undefined;
      },
      renderer(token) {
        return `<img src="${token.src}" data-width="${token.width}" />`;
      },
    },
  ],
});

const rawText = `aaa

![](imagelink){: width=50 }

ccc`;
const parsedText = marked.parse(rawText);

outputs

<img src="imagelink" data-width="50" /><p>=50 }</p>
<p>ccc</p>

Expected behavior

<p>aaa</p>
<p><img src="imagelink" data-width="50" /></p>
<p>bbb</p>

Thank you in advance.

@UziTech
Copy link
Member

UziTech commented Apr 30, 2024

It looks like your rule searches for a pattern anywhere in the source. It should only look for tokens at the beginning. Try adding ^ at the beginning of the regexp

@UziTech
Copy link
Member

UziTech commented May 1, 2024

It also looks like you are setting it as a block level token which won't be inside a <p> tag. You can change it to inline to parse it as an inline token.

import { Marked } from "marked";

import "./styles.css";

document.getElementById("app").innerHTML = `
<h1>Hello Vanilla!</h1>
<div>
  We use the same configuration as Parcel to bundle this sandbox, you can find more
  info about Parcel 
  <a href="https://parceljs.org" target="_blank" rel="noopener noreferrer">here</a>.
</div>`;
const marked = new Marked();
marked.use({
  extensions: [
    {
      name: "customImage",
      level: "inline",
      start: (source: string) => {
        return source.match(/!\[/)?.index;
      },
      tokenizer(source, tokens) {
        const rule = /^!\[\]\((.+?)\){: width=(\d+?) }/;
        const match = rule.exec(source);
        if (match) {
          const token = {
            type: "customImage",
            raw: match[0],
            src: match[1],
            width: match[2],
          };
          return token;
        }
        return undefined;
      },
      renderer(token) {
        return `<img src="${token.src}" data-width="${token.width}" />`;
      },
    },
  ],
});

const rawText = `aaa

inline ![](imagelink){: width=50 }

ccc`;
const parsedText = marked.parse(rawText);

console.log(parsedText);

@UziTech UziTech closed this as completed May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants