Skip to content

Commit 89224ad

Browse files
committed
attempting to improve syntax highlighter
1 parent 129fb8d commit 89224ad

File tree

1 file changed

+77
-44
lines changed

1 file changed

+77
-44
lines changed

site/_core/Prism.js

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@ Prism.languages.insertBefore('inside', 'attr-value',{
663663

664664
}(Prism));
665665

666+
666667
var graphqlCommon = {
667668
string: {
668669
pattern: /"(?:\\.|[^\\"])*"/,
@@ -671,20 +672,55 @@ var graphqlCommon = {
671672
number: /(?:\B-|\b)\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b/,
672673
boolean: /\b(?:true|false)\b/,
673674
variable: /\$[a-z_]\w*/i,
674-
directive: {
675-
pattern: /@[a-z_]\w*/i,
676-
alias: 'function'
677-
},
678675
comment: /#.*/,
676+
operator: /!|=|\.{3}/,
679677
punctuation: /[!(){|}[\]:=,]/
680678
};
681679

680+
var graphqlDirective = {
681+
pattern: /@[a-z_]\w*(\([\w\W]*?\))?/i,
682+
inside: {
683+
function: /@[a-z_]\w*/i,
684+
args: {
685+
pattern: /\([\w\W]*?\)/,
686+
inside: {
687+
arg: /[a-z_]\w*(?=\s*:)/i,
688+
...graphqlCommon
689+
}
690+
}
691+
}
692+
};
693+
682694
Prism.languages.graphql = {
695+
'schema-def': {
696+
pattern: /\bschema\b[^{]*{[^{}]*}/,
697+
inside: {
698+
keyword: /\bschema\b|[a-zA-Z_]\w*(?=\s*:)/,
699+
'type-name': {
700+
pattern: /(:[\s\[]*)[a-z_]\w*/i,
701+
lookbehind: true
702+
},
703+
directive: graphqlDirective,
704+
punctuation: graphqlCommon.punctuation
705+
}
706+
},
707+
'union-def': {
708+
pattern: /\bunion\b[^=]+=\s*[a-zA-Z_]\w*(?:\s*\|\s*[a-zA-Z_]\w*)*/,
709+
inside: {
710+
keyword: /\bunion\b/,
711+
'type-name': {
712+
pattern: /([=|]\s*)[a-z_]\w*/i,
713+
lookbehind: true
714+
},
715+
directive: graphqlDirective,
716+
punctuation: graphqlCommon.punctuation
717+
}
718+
},
683719
'type-def': {
684-
pattern: /\b(?:type|interface)\b[\w\W]+?{[\w\W]*?}/,
720+
pattern: /\b(?:type|interface|input|enum)\b[\w\W]+?{(?:[^{}]*|[^{}]*{[^{}]*}[^{}]*|[^{}]*{[^{}]*[^{}]*{[^{}]*}[^{}]*}[^{}]*)}/,
685721
inside: {
686722
fields: {
687-
pattern: /{[\w\W]*?}/,
723+
pattern: /{(?:[^{}]*|[^{}]*{[^{}]*}[^{}]*|[^{}]*{[^{}]*[^{}]*{[^{}]*}[^{}]*}[^{}]*)}/,
688724
inside: {
689725
argDefs: {
690726
pattern: /\([\w\W]*?\)/,
@@ -694,68 +730,65 @@ Prism.languages.graphql = {
694730
pattern: /(:[\s\[]*)[a-z_]\w*/i,
695731
lookbehind: true
696732
},
697-
directive: {
698-
pattern: /@[a-z_]\w*/i,
699-
alias: 'function'
700-
},
701-
rest: graphqlCommon
733+
directive: graphqlDirective,
734+
...graphqlCommon
702735
}
703736
},
737+
directive: graphqlDirective,
704738
'attr-name': {
705739
pattern: /[a-z_]\w*(?=\s*[:\(])/i,
740+
greedy: true,
706741
},
707742
'type-name': {
708743
pattern: /(:[\s\[]*)[a-z_]\w*/i,
709744
lookbehind: true
710745
},
711-
directive: {
712-
pattern: /@[a-z_]\w*/i,
713-
alias: 'function'
714-
},
715746
comment: /#.*/,
716747
punctuation: /[!{}\[\]:=,]/,
717748
}
718749
},
719-
keyword: /\b(?:type|interface|implements)\b/,
720-
'type-name': /[a-z_]\w*/i,
721-
string: /"(?:\\.|[^\\"])*"/,
722-
number: /(?:\B-|\b)\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b/,
723-
boolean: /\b(?:true|false)\b/,
724-
variable: /\$[a-z_]\w*/i,
725-
directive: {
726-
pattern: /@[a-z_]\w*/i,
727-
alias: 'function'
728-
},
729-
comment: /#.*/,
750+
keyword: /\b(?:type|interface|implements|input|enum)\b/,
751+
directive: graphqlDirective,
752+
...graphqlCommon,
753+
754+
// 'type-name': /[a-z_]\w*/i,
730755
}
731756
},
732-
string: /"(?:\\.|[^\\"])*"/,
733-
number: /(?:\B-|\b)\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b/,
734-
boolean: /\b(?:true|false)\b/,
735-
variable: /\$[a-z_]\w*/i,
736-
directive: {
737-
pattern: /@[a-z_]\w*/i,
738-
alias: 'function'
739-
},
757+
// string: /"(?:\\.|[^\\"])*"/,
758+
// number: /(?:\B-|\b)\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b/,
759+
// boolean: /\b(?:true|false)\b/,
760+
// variable: /\$[a-z_]\w*/i,
761+
// directive: {
762+
// pattern: /@[a-z_]\w*/i,
763+
// alias: 'function'
764+
// },
765+
directive: graphqlDirective,
740766
'attr-name': /[a-z_]\w*(?=\s*:)/i,
741767
'keyword': [
742768
{
743769
pattern: /(fragment\s+(?!on)[a-z_]\w*\s+|\.\.\.\s*)on\b/,
744770
lookbehind: true
745771
},
746-
/\b(?:query|mutation|subscription|fragment|extend)\b/
772+
/\b(?:query|mutation|subscription|fragment|extend|scalar)\b/
747773
],
748-
'operator': /!|=|\.{3}/,
749-
'punctuation': /[!(){}\[\]:=,]/,
750-
comment: /#.*/,
774+
...graphqlCommon,
775+
// 'operator': /!|=|\.{3}/,
776+
// 'punctuation': /[!(){}\[\]:=,]/,
777+
// comment: /#.*/,
751778
// 'enum': /[a-z_]\w*/i
752779
};
753780

754781
Prism.languages.json = {
755-
'attr-name': /"(?:\\.|[^\\"])*"(?=\s*:)/i,
756-
'string': /"(?:\\.|[^\\"])*"/,
757-
'boolean': /\b(?:true|false)\b/,
758-
'keyword': /\bnull\b/,
759-
'number': /(?:\B-|\b)\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b/,
760-
'punctuation': /[{}[\],:]/,
782+
'attr-name': {
783+
pattern: /"(?:\\.|[^\\"])*"(?=\s*:)/i,
784+
greedy: true
785+
},
786+
string: {
787+
pattern: /"(?:\\.|[^\\"])*"/,
788+
greedy: true
789+
},
790+
boolean: /\b(?:true|false)\b/,
791+
keyword: /\bnull\b/,
792+
number: /(?:\B-|\b)\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b/,
793+
punctuation: /[{}[\],:]/,
761794
};

0 commit comments

Comments
 (0)