prism-cpp definition error

Created on 21 Sep 2018  路  4Comments  路  Source: PrismJS/prism

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.

https://github.com/PrismJS/prism/blob/e529edd8a6efd0a8a21cd41412b72019bc522307/components/prism-cpp.js#L1

Most helpful comment

@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>

All 4 comments

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>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

RobLoach picture RobLoach  路  9Comments

RunDevelopment picture RunDevelopment  路  5Comments

benjaminBrownlee picture benjaminBrownlee  路  4Comments

TheZoker picture TheZoker  路  9Comments

jaune162 picture jaune162  路  6Comments