Update mdx language definition

This commit is contained in:
Remco Haszing 2022-05-06 18:25:31 +02:00
parent 7d2065d55d
commit e87ef8ef11
No known key found for this signature in database
GPG key ID: 40D9F5FE9155FD3C

View file

@ -8,58 +8,111 @@ import type { languages } from '../../fillers/monaco-editor-core';
export const conf: languages.LanguageConfiguration = { export const conf: languages.LanguageConfiguration = {
comments: { comments: {
blockComment: ['{/*', '*/}'] blockComment: ['{/*', '*/}']
} },
brackets: [['{', '}']]
}; };
export const language = <languages.IMonarchLanguage>{ export const language = <languages.IMonarchLanguage>{
defaultToken: '', defaultToken: '',
tokenPostfix: '.mdx', tokenPostfix: '.mdx',
escapes: /\\(?:["'\\abfnrtv]|x[\dA-Fa-f]{1,4}|u[\dA-Fa-f]{4}|U[\dA-Fa-f]{8})/, control: /[!#()*+.[\\\]_`{}\-]/,
bracket_open: ['{'], escapes: /\\@control/,
single_quote: ["'"],
double_quote: ['"'],
tokenizer: { tokenizer: {
root: [ root: [
[/^---$/, { token: 'meta.content', next: '@frontmatter', nextEmbedded: 'yaml' }],
[/^\s{4}.*$/, { token: 'variable.source' }],
[/^\s*import/, { token: 'keyword', next: '@import', nextEmbedded: 'js' }], [/^\s*import/, { token: 'keyword', next: '@import', nextEmbedded: 'js' }],
[/<\w+/, { token: 'keyword', next: '@jsx' }], [/^\s*export/, { token: 'keyword', next: '@export', nextEmbedded: 'js' }],
[/<\/?\w+>/, { token: 'keyword' }], [/<\w+/, { token: 'type.identifier', next: '@jsx' }],
[/\*\*.+\*\*/, 'strong'], [/<\/?\w+>/, 'type.identifier'],
[/{/, { token: 'delimiter.bracket', nextEmbedded: 'js' }], [
{ include: 'expression' } /^(\s*)(>*\s*)(#{1,6}\s)/,
[{ token: 'white' }, { token: 'comment' }, { token: 'keyword', next: '@header' }]
],
[/^(\s*)(>*\s*)([*+-])(\s+)/, ['white', 'comment', 'keyword', 'white']],
[/^(\s*)(>*\s*)(\d{1,9}\.)(\s+)/, ['white', 'comment', 'number', 'white']],
[/^(\s*)(>*\s*)(\d{1,9}\.)(\s+)/, ['white', 'comment', 'number', 'white']],
[/^(\s*)(>*\s*)(-{3,}|\*{3,}|_{3,})$/, ['white', 'comment', 'keyword']],
[/`{3,}(\s.*)?$/, { token: 'string', next: '@codeblock_backtick' }],
[/~{3,}(\s.*)?$/, { token: 'string', next: '@codeblock_tilde' }],
[
/`{3,}(\S+).*$/,
{ token: 'string', next: '@codeblock_highlight_backtick', nextEmbedded: '$1' }
],
[
/~{3,}(\S+).*$/,
{ token: 'string', next: '@codeblock_highlight_tilde', nextEmbedded: '$1' }
],
[/^(\s*)(-{4,})$/, ['white', 'comment']],
[/^(\s*)(>+)/, ['white', 'comment']],
{ include: 'content' }
],
content: [
[
/(\[)(.+)(]\()(.+)(\s+".*")(\))/,
['', 'string.link', '', 'type.identifier', 'string.link', '']
],
[/(\[)(.+)(]\()(.+)(\))/, ['', 'type.identifier', '', 'string.link', '']],
[/(\[)(.+)(]\[)(.+)(])/, ['', 'type.identifier', '', 'type.identifier', '']],
[/(\[)(.+)(]:\s+)(\S*)/, ['', 'type.identifier', '', 'string.link']],
[/(\[)(.+)(])/, ['', 'type.identifier', '']],
[/`.*`/, 'variable.source'],
[/_/, { token: 'emphasis', next: '@emphasis_underscore' }],
[/\*(?!\*)/, { token: 'emphasis', next: '@emphasis_asterisk' }],
[/\*\*/, { token: 'strong', next: '@strong' }],
[/{/, { token: 'delimiter.bracket', next: '@expression', nextEmbedded: 'js' }]
], ],
import: [[/'\s*(;|$)/, { token: 'string', next: '@pop', nextEmbedded: '@pop' }]], import: [[/'\s*(;|$)/, { token: 'string', next: '@pop', nextEmbedded: '@pop' }]],
expression: [[/}/, { token: 'delimiter.bracket', nextEmbedded: '@pop' }]], expression: [
[/{/, { token: 'delimiter.bracket', next: '@expression' }],
[/}/, { token: 'delimiter.bracket', next: '@pop', nextEmbedded: '@pop' }]
],
export: [[/^\s*$/, { token: 'delimiter.bracket', next: '@pop', nextEmbedded: '@pop' }]],
jsx: [ jsx: [
[/\w+=/, { token: 'delimiter.bracket', next: '@jsx_expression' }], [/\s+/, ''],
[/\/?>/, { token: 'keyword', next: '@pop' }] [/(\w+)(=)("(?:[^"\\]|\\.)*")/, ['attribute.name', 'operator', 'string']],
[/(\w+)(=)('(?:[^'\\]|\\.)*')/, ['attribute.name', 'operator', 'string']],
[/(\w+(?=\s|>|={|$))/, ['attribute.name']],
[/={/, { token: 'delimiter.bracket', next: '@expression', nextEmbedded: 'js' }],
[/>/, { token: 'type.identifier', next: '@pop' }]
], ],
jsx_expression: [ header: [
[ [/.$/, { token: 'keyword', next: '@pop' }],
/["'{]/, { include: 'content' },
{ [/./, { token: 'keyword' }]
cases: {
'@bracket_open': {
token: 'delimiter.bracket',
next: '@expression',
nextEmbedded: 'js'
},
'@double_quote': { token: 'string', next: '@string_double' },
'@single_quote': { token: 'string', next: '@string_single' }
}
}
]
], ],
string_double: [ strong: [
[/[^"\\]+/, 'string'], [/\*\*/, { token: 'strong', next: '@pop' }],
[/@escapes/, 'string.escape'], { include: 'content' },
[/\\./, 'string.escape.invalid'], [/./, { token: 'strong' }]
[/"/, 'string', '@pop']
], ],
string_single: [ emphasis_underscore: [
[/[^'\\]+/, 'string'], [/_/, { token: 'emphasis', next: '@pop' }],
[/@escapes/, 'string.escape'], { include: 'content' },
[/\\./, 'string.escape.invalid'], [/./, { token: 'emphasis' }]
[/'/, 'string', '@pop'] ],
emphasis_asterisk: [
[/\*(?!\*)/, { token: 'emphasis', next: '@pop' }],
{ include: 'content' },
[/./, { token: 'emphasis' }]
],
frontmatter: [[/^---$/, { token: 'meta.content', nextEmbedded: '@pop', next: '@pop' }]],
codeblock_highlight_backtick: [
[/\s*`{3,}\s*$/, { token: 'string', next: '@pop', nextEmbedded: '@pop' }],
[/.*$/, 'variable.source']
],
codeblock_highlight_tilde: [
[/\s*~{3,}\s*$/, { token: 'string', next: '@pop', nextEmbedded: '@pop' }],
[/.*$/, 'variable.source']
],
codeblock_backtick: [
[/\s*`{3,}\s*$/, { token: 'string', next: '@pop' }],
[/.*$/, 'variable.source']
],
codeblock_tilde: [
[/\s*~{3,}\s*$/, { token: 'string', next: '@pop' }],
[/.*$/, 'variable.source']
] ]
} }
}; };