-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gjs): consistently highlight gjs blocks
additionally, there are more tests to ensure that this happens. also, glimmer-javascript is now a registered language with highlight.js later, in a breaking version, gjs will be the preferred language tag, instead of highjacking js
- Loading branch information
1 parent
158a348
commit abfdd2b
Showing
9 changed files
with
516 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,103 @@ | ||
import { expect } from 'vitest'; | ||
import hljs from 'highlight.js'; | ||
import { registerLanguage } from 'highlightjs-glimmer'; | ||
import { setup } from 'highlightjs-glimmer'; | ||
|
||
registerLanguage(hljs); | ||
setup(hljs); | ||
|
||
import prettier from 'prettier'; | ||
|
||
// Used to resolve indentation issues. | ||
// We don't care about that in these tests -- we only care about tag identification. | ||
export function format(code) { | ||
return prettier.format(code, { parser: 'html', htmlWhitespaceSensitivity: 'ignore' }); | ||
} | ||
|
||
export function formattedEquals(actual, expected) { | ||
expect(format(actual)).toEqual(format(expected)); | ||
} | ||
|
||
export function parse(code, lang = 'glimmer') { | ||
return hljs.highlight(code, { language: lang }).value; | ||
} | ||
|
||
export function glimmer(...children) { | ||
return `<span class="language-glimmer">${children.join('')}</span>`; | ||
} | ||
|
||
export const templateTags = { | ||
get begin() { | ||
return tag('tag', ['<', tag('title', 'template'), '>']); | ||
}, | ||
get end() { | ||
return tag('tag', ['</', tag('title', 'template'), '>']); | ||
}, | ||
}; | ||
|
||
export function template(...content) { | ||
return list(templateTags.begin, '\n', ...indentLines(content), '\n', templateTags.end); | ||
} | ||
|
||
export function selfClosing(name, attributes = []) { | ||
return tag('tag', ['<', tag('title', name), ...attributes, ' />']); | ||
} | ||
|
||
export function indentLines(lines, amount = 2) { | ||
let indent = Array(amount + 1).join(' '); | ||
|
||
return lines.map((line) => { | ||
return `${indent}${line}`; | ||
}); | ||
} | ||
|
||
export function element(name, content = []) { | ||
return list( | ||
tag('tag', ['<', tag('title', name), '>']), | ||
'\n', | ||
...indentLines(content), | ||
'\n', | ||
tag('tag', ['</', tag('title', name), '>']) | ||
); | ||
} | ||
|
||
export function mustache(...content) { | ||
return tag('punctuation mustache', ['{{', ...content, '}}']); | ||
} | ||
|
||
export function arg(name) { | ||
return list(tag('punctuation', '@'), tag('params', name)); | ||
} | ||
|
||
export const keyword = { | ||
export: tag('keyword', 'export'), | ||
const: tag('keyword', 'const'), | ||
default: tag('keyword', 'default'), | ||
}; | ||
|
||
export const operator = { | ||
equals: tag('operator', '='), | ||
}; | ||
|
||
export function string(content) { | ||
return tag('string', ['"', content, '"']); | ||
} | ||
|
||
export const tags = { | ||
template, | ||
mustache, | ||
element, | ||
selfClosing, | ||
arg, | ||
keyword, | ||
operator, | ||
string, | ||
}; | ||
|
||
export function tag(klass, children = []) { | ||
let _children = Array.isArray(children) ? children : [children]; | ||
|
||
return `<span class="hljs-${klass}">${_children.join('')}</span>`; | ||
} | ||
|
||
export function list(children) { | ||
export function list(...children) { | ||
return children.join(''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.