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

Code block ignores newlines #2741

Closed
nmime opened this issue Feb 22, 2023 · 9 comments
Closed

Code block ignores newlines #2741

nmime opened this issue Feb 22, 2023 · 9 comments

Comments

@nmime
Copy link

nmime commented Feb 22, 2023

Marked version: 4.2.12

Describe the bug
Line break is ignored in code block.
with parseInline()

To Reproduce
```
// Example of code
console.log("Hello world!");
```

converted to
<code>// Example of code console.log("Hello world!");</code>

Expected behavior
<code>// Example of code
console.log("Hello world!");</code>

@calculuschild
Copy link
Contributor

calculuschild commented Feb 22, 2023

Are you using parseInline() in a custom extension? In general, parseInline() only handles single lines of content. If you want to parse the whole code block including line breaks, use parse(). Similarly, you probably want to make sure your Tokenizer is using this.lexer.blockTokens instead of this.lexer.inline.

If you want to share your extension code so far we can take a look and provide tips to get you unstuck.

@nmime
Copy link
Author

nmime commented Feb 22, 2023

I am using parseInline().
I need to output only these tags:
<b>
<i>
<u>
<s>
<del>
<a>
<code>

@calculuschild
Copy link
Contributor

calculuschild commented Feb 22, 2023

Could you provide the entire code you have tried for your extension so far?

@nmime
Copy link
Author

nmime commented Feb 22, 2023

just used parseInline ;)

@UziTech
Copy link
Member

UziTech commented Feb 22, 2023

As @calculuschild said parseInline is for inline content only. Fenced code block are block content. This looks like it is working as expected.

@nmime
Copy link
Author

nmime commented Feb 22, 2023

ok, how can get output with only specified tags?

@UziTech
Copy link
Member

UziTech commented Feb 23, 2023

You can create a tokenizer that is a noop for any tokens you don't want.

Something like:

const tokenizer = {
  heading() {},
  hr() {},
  blockquote() {},
  list() {},
  table() {},
  lheading() {}
};
marked.use({tokenizer});

@nmime
Copy link
Author

nmime commented Feb 23, 2023

I passed like that

    fences() {},
    heading() {},
    hr() {},
    blockquote() {},
    list() {},
    def() {},
    table() {},
    lheading() {},
    paragraph() {}

this helped, but how to remove the <p> at the beginning and end?

@UziTech
Copy link
Member

UziTech commented Feb 23, 2023

If you are parsing as block tokens every token has to be in a block token. The easiest way to remove them is just to remove them after running through marked.

let html = marked.parse(markdown);
html = html.replace(/^\s*<p>([^]*)<\/p>\s*$/, "$1");

Keep in mind that html is valid markdown so if you actually do not want to allow someone to output <p> tags you will have to use some other library like dompurify to remove them.

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

No branches or pull requests

3 participants