Loading the C++ language definition yields the following error in my browser (Chrome v69.0.3497.100 64-bit):
Uncaught TypeError: Cannot set property 'keyword' of undefined
at Object.extend (docsify.min.js:1)
at prism-cpp.min.js:1
and therefore C++ syntax is not highlighted properly.
This can be fixed by extending csharp instead of c, but I don't know whether this comes with side-effects.
My bad; it seems to be because prism-c needs to be loaded beforehand, in order for the prism-cpp syntax to work properly. Is that normal?
<script src="//unpkg.com/prismjs/components/prism-c.min.js"></script>
<script src="//unpkg.com/prismjs/components/prism-cpp.min.js"></script>
@sheljohn
Yes, that's normal. Most Prism languages have some requirements, meaning languages which have to be loaded beforehand.
In our case: cpp depends on c. But we also have to consider transitive dependencies, because c depends on clike. This means that the load order has to be clike before c before cpp.
If you want to know the dependencies of a language (not including transitive ones), select the language on the Prism example page. If the language has any dependencies, they will be displayed like this: (for C++)
__Dependencies:__ The following dependencies need to be loaded before this component:
c.
Alternatively, you can open components.json which holds the dependencies of all languages in the require property.
It might be good to consider packaging all dependencies in each .js file, and protecting definitions within if blocks to avoid redefining things. Would it be of interest if I contributed code to do this automatically?
@sheljohn
Dependencies should not be included in each .js files because this will increase the overall amount of bytes which have to be loaded.
As an example, let's say we want to highlight C++ and Java code. Both languages require clike, so both would include it. This means that we might be loading a lot of redundant language definitions.
I suggest using the Autoloader plugin. It will automatically load necessary language definitions and all dependencies for you. (You don't even need to include C++ yourself.)
<script src="https://unpkg.com/[email protected]/components/prism-core.min.js"></script>
<script src="https://unpkg.com/[email protected]/plugins/autoloader/prism-autoloader.min.js"></script>
<script>
Prism.plugins.autoloader.languages_path = 'https://unpkg.com/[email protected]/components/'
</script>
Most helpful comment
@sheljohn
Dependencies should not be included in each
.jsfiles because this will increase the overall amount of bytes which have to be loaded.As an example, let's say we want to highlight C++ and Java code. Both languages require
clike, so both would include it. This means that we might be loading a lot of redundant language definitions.I suggest using the Autoloader plugin. It will automatically load necessary language definitions and all dependencies for you. (You don't even need to include C++ yourself.)