Open therealpaulgg opened 5 years ago
Hi! I just realized that I never answered your issue. Sorry!
First of all: thank you for reporting this.
After you opened the issued I did some digging. However, I found no straightforward solution to the problem. I attempted to make this plugin compatible to babel-plugin-prismjs, but with no success.
How do you make sure that the languages are included in the Webpack result when you use your code snippet?
The workaround solution that I used is definitely not ideal and it is NOT by any means an elegant solution, but it works and I am currently not sure on how to load the languages automatically without calling that PrismJS function (But it breaks in webpack :/).
To load languages I had to import each language from prismjs individually. Many languages in prismJS have dependencies too, so the order of the import statements actually matters. For instance, JavaScript, C, and Java require the clike
language to be loaded beforehand otherwise the application will throw an error.
I'll include some sample code that I used below (it is very long).
Main downside from this from a performance standpoint is that it increases the overall bundle size of the application by a significant amount.
@PaulGG: You can use your approach (manually listing the language dependencies) with this plugin, too:
import MarkdownIt from 'markdown-it';
import prism from 'markdown-it-prism';
import "prismjs/components/prism-abap"
import "prismjs/components/prism-abnf"
import "prismjs/components/prism-clike"
import "prismjs/components/prism-java"
import "prismjs/components/prism-javadoclike"
import "prismjs/components/prism-javadoc"
import "prismjs/components/prism-javastacktrace"
function component() {
const md = new MarkdownIt();
md.use(prism);
const element = document.createElement('div');
element.innerHTML = md.render(`
Here is some *code*:
\`\`\`java
public class Test {
public void foo() {}
}
\`\`\`
`);
return element;
}
document.body.appendChild(component());
after npx webpack --mode=development
, the div
on the webpage has this content:
<div><p>Here is some <em>code</em>:</p>
<pre class=" language-java"><code class=" language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
</div>
But this is not very helpful, is it? I will add a note about it in the README anyway.
has this problem sovled?
@winner106 my last post still describes the state of this issue: you can use this plugin, but you will have to load the languages you need manually.
This is not something we can fix in the scope of this plugin. Prism loads the languages it needs dynamically, and it has no direct way of knowing ahead of time which languages will be needed. So you either have to preload the languages you know you need, or preemptively import all.
This plugin does not work on webpack applications. After doing some testing and looking into the source code of this project, Prism themselves say that this is because you should not call loadLanguages() in a webpack application, and that a babel plugin should be used instead. That plugin seems to be integral to this plugin, and so I was getting the same errors using this plugin that I was when I was simply using Prism without the plugin, but attempting to use loadLanguages().
I am using this plugin on a node.js express application, and it renders server-side with no issues (no webpack). However, when I am building my vue.js app and need to add preview functionality, it does not work, probably because I am using webpack.
If there would be any way to fix it so others could avoid the trouble of having to manually add prism to markdown, that would be great.
I am including source code below that I am using that is based off of the official markdown-it docs using highlight-js here https://markdown-it.github.io/markdown-it/, but instead using Prismjs. I hope this helps anyone who is trying to accomplish what I was. This is the code I'll be using for now.