Skip to content

Commit

Permalink
Tools global highlighter (#12)
Browse files Browse the repository at this point in the history
* using cli lang and updated to accept mlem

* supporting dvc as well
  • Loading branch information
yathomasi authored May 19, 2022
1 parent 947775b commit 508b8c2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby-theme-iterative/config/prismjs/dvc-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const argsRegex = new RegExp(/\-{1,2}[a-zA-Z-]*/, 'ig')
// Make sure the $ part of the command prompt in shell
// examples isn't copiable by making it an 'input' token.
Prism.hooks.add('after-tokenize', env => {
if (['dvc', 'cml'].includes(env.language) && Array.isArray(env.tokens)) {
if (['dvc', 'cli'].includes(env.language) && Array.isArray(env.tokens)) {
for (const token of env.tokens) {
if (token.type === 'line' && /^\$\s+$/.test(token.content[0])) {
const old = token.content[0]
Expand Down
27 changes: 8 additions & 19 deletions packages/gatsby-theme-iterative/config/prismjs/dvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { bash } = Prism.languages

const dvc = require('./dvc-commands')
const cml = require('./cml-commands')
const mlem = require('./mlem-commands')

// Command arrays are intentionally reverse sorted
// to prevent shorter matches before longer ones
Expand All @@ -35,7 +36,7 @@ const git = [

const beforeCommand = String.raw`(\$[\s(]+|;\s*)`

Prism.languages.dvc = {
Prism.languages.cli = {
line: {
pattern: /(?<=(^|\n))\$[\s\S]*?[^\\](:?\n|$)/,
inside: {
Expand All @@ -46,30 +47,16 @@ Prism.languages.dvc = {
greedy: true,
lookbehind: true
},
git: {
cml: {
pattern: new RegExp(
String.raw`${beforeCommand}\b(?:git (?:${git.join('|')}))\b`
String.raw`${beforeCommand}\b(?:cml (?:${cml.join('|')}))\b`
),
greedy: true,
lookbehind: true
},
command: {
pattern: new RegExp(String.raw`${beforeCommand}\b[a-zA-Z0-9\-_]+\b`),
greedy: true,
lookbehind: true
},
...bash
}
},
comment: bash.comment
}
Prism.languages.cml = {
line: {
pattern: /(?<=(^|\n))\$[\s\S]*?[^\\](:?\n|$)/,
inside: {
cml: {
mlem: {
pattern: new RegExp(
String.raw`${beforeCommand}\b(?:cml (?:${cml.join('|')}))\b`
String.raw`${beforeCommand}\b(?:mlem (?:${mlem.join('|')}))\b`
),
greedy: true,
lookbehind: true
Expand All @@ -91,3 +78,5 @@ Prism.languages.cml = {
},
comment: bash.comment
}

Prism.languages.dvc = Prism.languages.cli
14 changes: 14 additions & 0 deletions packages/gatsby-theme-iterative/config/prismjs/mlem-commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = [
'init',
'list',
'pprint',
'create',
'serve',
'deploy',
'types',
'link',
'clone',
'import',
'pack',
'apply'
]
4 changes: 4 additions & 0 deletions packages/gatsby-theme-iterative/config/prismjs/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const dvc = require('./dvc-commands')
const cml = require('./cml-commands')
const mlem = require('./mlem-commands')
const Prism = require('prismjs')

Prism.languages.usage = {
Expand All @@ -11,6 +12,9 @@ Prism.languages.usage = {
cml: {
pattern: new RegExp(`cml (?:${cml.join('|')})`)
},
mlem: {
pattern: new RegExp(`mlem (?:${mlem.join('|')})`)
},
usage: {
pattern: /(^|\n)\s*(usage|positional arguments|optional arguments)/
},
Expand Down
6 changes: 5 additions & 1 deletion packages/gatsby-theme-iterative/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ module.exports = ({
options: {
icon: linkIcon,
// Pathname can also be array of paths. eg: ['docs/command-reference;', 'docs/api']
pathname: ['docs/command-reference', `docs/ref`]
pathname: [
'docs/command-reference',
`docs/ref`,
'docs/cli-reference'
]
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@
}

.token.dvc,
.token.cml {
.token.cml,
.token.mlem {
color: #56b1d0;
font-weight: bold;
}
Expand Down

0 comments on commit 508b8c2

Please sign in to comment.