bent10 / marked-extensions

Marked extensions workspace
https://www.npmjs.com/search?q=keywords:stilearning-marked-extensions
MIT License
32 stars 5 forks source link

Why does the markedFootnote plugin affect the parsing of the markedHighlight highlighting plugin? #27

Closed jiaopengzi closed 9 months ago

jiaopengzi commented 9 months ago
import { Marked } from 'marked'
import { markedHighlight } from 'marked-highlight' // Code highlighting
import markedFootnote from 'marked-footnote' // Footnotes
import hljs from 'highlight.js'
import type { SynchronousOptions } from 'marked-highlight' // Code highlighting options

const optionHighlight: SynchronousOptions = {
  langPrefix: 'hljs language-',
  highlight(code, lang) {
    const language = hljs.getLanguage(lang) ? lang : 'plaintext'
    return hljs.highlight(code, { language }).value
  },
}

const marked = new Marked()

marked.use(
  markedFootnote(),
  markedHighlight(optionHighlight),
)

console.log("has content====>", marked.parse(`
# heading1
\`\`\`javascript
const highlight = "code";
\`\`\`
`))

console.log("no cotent ====>", marked.parse(`
\`\`\`javascript
const highlight = "code";
\`\`\`
`))

/*
You update the plugins very timely. I discovered an issue while using it.
1. When there is no content before a block of code, marked parsing causes the code highlighting plugin highlight.js to fail.
2. When there is content before a block of code, marked parsing works fine and the code highlighting plugin highlight.js works normally.
3. When there is no content before inline code, comment out line 18 markedFootnote plugin, marked parsing works fine, and the code highlighting plugin highlight.js works normally.

Why does the markedFootnote plugin affect the highlight.js plugin? Could you please take a look for me? Thank you.
I've also tried to replace the order of the plugins in the use method but still couldn't solve the problem.

When there is content before a code block, correct parsing result
has content====> <h1>heading1</h1>
<pre><code class="hljs language-javascript"><span class="hljs-keyword">const</span> highlight = <span class="hljs-string">&quot;code&quot;</span>;
</code></pre>

When there is no content before a code block, incorrect parsing result.
no cotent ====> <pre><code class="hljs language-javascript">const highlight = &quot;code&quot;;
</code></pre>

When the markedFootnote plugin is commented out, all results are parsed correctly.

When there is content before a code block, correct parsing result
has content====> <h1>heading1</h1>
<pre><code class="hljs language-javascript"><span class="hljs-keyword">const</span> highlight = <span class="hljs-string">&quot;code&quot;</span>;
</code></pre>

When there is no content before a code block, correct parsing result
cotent ====> <pre><code class="hljs language-javascript"><span class="hljs-keyword">const</span> highlight = <span class="hljs-string">&quot;code&quot;</span>;
</code></pre>
*/

bug description

version

You update the plugins very timely. I discovered an issue while using it.

  1. When there is no content before a block of code, marked parsing causes the code highlighting plugin highlight.js to fail.
  2. When there is content before a block of code, marked parsing works fine and the code highlighting plugin highlight.js works normally.
  3. When there is no content before inline code, comment out line 18 markedFootnote plugin, marked parsing works fine, and the code highlighting plugin highlight.js works normally.
  4. The above TypeScript code can directly reproduce the bug.

Why does the markedFootnote plugin affect the highlight.js plugin? Could you please take a look for me? Thank you. I've also tried to replace the order of the plugins in the use method but still couldn't solve the problem.

When the markedFootnote plugin is commented out, all results are parsed correctly.

bent10 commented 9 months ago

Ah, thank you for pointing out that issue. It's actually a known issue, but I accidentally overlooked it 😅

I have released v1.2.1 to address it. Please check it out 🚀

jiaopengzi commented 9 months ago

so cool thanks.