Skip to content

Commit

Permalink
fix(twig): encapsulate twig highlighter in a function
Browse files Browse the repository at this point in the history
  • Loading branch information
jankal committed Sep 22, 2023
1 parent 59e5a34 commit 7334750
Showing 1 changed file with 46 additions and 44 deletions.
90 changes: 46 additions & 44 deletions components/prism-twig.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
Prism.languages.twig = {
'comment': /^\{#[\s\S]*?#\}$/,

'tag-name': {
pattern: /(^\{%-?\s*)\w+/,
lookbehind: true,
alias: 'keyword'
},
'delimiter': {
pattern: /^\{[{%]-?|-?[%}]\}$/,
alias: 'punctuation'
},

'string': {
pattern: /("|')(?:\\.|(?!\1)[^\\\r\n])*\1/,
inside: {
'punctuation': /^['"]|['"]$/
}
},
'keyword': /\b(?:even|if|odd)\b/,
'boolean': /\b(?:false|null|true)\b/,
'number': /\b0x[\dA-Fa-f]+|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:[Ee][-+]?\d+)?/,
'operator': [
{
pattern: /(\s)(?:and|b-and|b-or|b-xor|ends with|in|is|matches|not|or|same as|starts with)(?=\s)/,
lookbehind: true
},
/[=<>]=?|!=|\*\*?|\/\/?|\?:?|[-+~%|]/
],
'punctuation': /[()\[\]{}:.,]/
};

Prism.hooks.add('before-tokenize', function (env) {
if (env.language !== 'twig') {
return;
}

var pattern = /\{(?:#[\s\S]*?#|%[\s\S]*?%|\{[\s\S]*?\})\}/g;
Prism.languages['markup-templating'].buildPlaceholders(env, 'twig', pattern);
});

Prism.hooks.add('after-tokenize', function (env) {
Prism.languages['markup-templating'].tokenizePlaceholders(env, 'twig');
});
(function (Prism) {
Prism.languages.twig = {

Check warning on line 2 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 1 tab but found 2 spaces
'comment': /^\{#[\s\S]*?#\}$/,

Check failure on line 3 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Mixed spaces and tabs

Check warning on line 4 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
'tag-name': {

Check failure on line 5 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Mixed spaces and tabs
pattern: /(^\{%-?\s*)\w+/,

Check failure on line 6 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Mixed spaces and tabs
lookbehind: true,

Check failure on line 7 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Mixed spaces and tabs
alias: 'keyword'

Check failure on line 8 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Mixed spaces and tabs
},

Check failure on line 9 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Mixed spaces and tabs
'delimiter': {

Check failure on line 10 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Mixed spaces and tabs
pattern: /^\{[{%]-?|-?[%}]\}$/,

Check failure on line 11 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Mixed spaces and tabs
alias: 'punctuation'

Check failure on line 12 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Mixed spaces and tabs
},

Check failure on line 13 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Mixed spaces and tabs

Check warning on line 14 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
'string': {
pattern: /("|')(?:\\.|(?!\1)[^\\\r\n])*\1/,
inside: {
'punctuation': /^['"]|['"]$/
}
},
'keyword': /\b(?:even|if|odd)\b/,
'boolean': /\b(?:false|null|true)\b/,
'number': /\b0x[\dA-Fa-f]+|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:[Ee][-+]?\d+)?/,
'operator': [
{
pattern: /(\s)(?:and|b-and|b-or|b-xor|ends with|in|is|matches|not|or|same as|starts with)(?=\s)/,
lookbehind: true
},
/[=<>]=?|!=|\*\*?|\/\/?|\?:?|[-+~%|]/
],
'punctuation': /[()\[\]{}:.,]/
};

Check warning on line 32 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 1 tab but found 2 spaces

Check warning on line 33 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
Prism.hooks.add('before-tokenize', function (env) {

Check warning on line 34 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 1 tab but found 2 spaces
if (env.language !== 'twig') {
return;
}

Check warning on line 38 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
var pattern = /\{(?:#[\s\S]*?#|%[\s\S]*?%|\{[\s\S]*?\})\}/g;
Prism.languages['markup-templating'].buildPlaceholders(env, 'twig', pattern);
});

Check warning on line 41 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 1 tab but found 2 spaces

Check warning on line 42 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
Prism.hooks.add('after-tokenize', function (env) {

Check warning on line 43 in components/prism-twig.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 1 tab but found 2 spaces
Prism.languages['markup-templating'].tokenizePlaceholders(env, 'twig');
});
}(Prism));

0 comments on commit 7334750

Please sign in to comment.