vue-prism-editor icon indicating copy to clipboard operation
vue-prism-editor copied to clipboard

"Cannot read property 'rest' of undefined" running code from README

Open tansaku opened this issue 3 years ago • 7 comments

Using the code from the readme I get this error embedding the editor in a nuxt app

Cannot read property 'rest' of undefined

	tokenize: function(text, grammar) {
		var rest = grammar.rest;  // <---------- error here
		if (rest) {
			for (var token in rest) {
				grammar[token] = rest[token];
			}

I can see the editor if I adjust the highlighter method to return null, but it looks a bit messed up

Screenshot 2021-06-04 at 17 11 19

any ideas?

tansaku avatar Jun 04 '21 16:06 tansaku

the code sample shows up if I adjust prism-core.js like so

	tokenize: function(text, grammar) {
		if(grammar !== undefined)
		{
			var rest = grammar.rest;
			if (rest) {
				for (var token in rest) {
					grammar[token] = rest[token];
				}

				delete grammar.rest;
			}
		}

but the line numbers don't look great:

Screenshot 2021-06-04 at 17 17 45

tansaku avatar Jun 04 '21 16:06 tansaku

For reference I'm running with node v14.17.0

tansaku avatar Jun 04 '21 16:06 tansaku

Same issue, doesn't display anything at all

preview

WardPearce avatar Jun 13 '21 14:06 WardPearce

Looking into it, i was getting this error because languages didn't have the attributes I expected.

e.g. languages.plain isn't an option

WardPearce avatar Jun 13 '21 14:06 WardPearce

Hey! Can you provide an example using codesandbox or stackblitz. Thanks :) i will re-open this issue once you have it.

koca avatar Feb 09 '22 23:02 koca

Hey folks.

These kind of errors appear when you do not wrap your code editor component in <ClientOnly>.

Check out the nuxtjs example: https://codesandbox.io/s/nuxt-vue-prism-editor-62e06

surister avatar Feb 12 '22 12:02 surister

Hey @surister thanks a lot for the codesandbox link. I will check it and try to fix the issue. I will let you guys know :)

koca avatar Feb 17 '22 22:02 koca