Skip to content

Commit

Permalink
feat: add code language suggestions for code block
Browse files Browse the repository at this point in the history
  • Loading branch information
purocean committed Aug 4, 2023
1 parent 7a4b829 commit de30946
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/renderer/plugins/editor-md-syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export default {
{ label: '/ __ Bold', insertText: '__$1__' },
{ label: '/ ~~ Delete', insertText: '~~$1~~' },
{ label: '/ == Mark', insertText: '==$1==' },
{ label: '/ ``` Fence', insertText: '```$1\n```\n' },
{ label: '/ ``` Fence', insertText: '```$1\n$2\n```\n' },
{ label: '/ ||| Table', insertText: '| ${1:TH} | ${2:TH} | ${3:TH} |\n| -- | -- | -- |\n| TD | TD | TD |' },
{ label: '/ ||| Small Table', insertText: '| ${1:TH} | ${2:TH} | ${3:TH} |\n| -- | -- | -- |\n| TD | TD | TD |\n{.small}' },
{ label: '/ --- Horizontal Line', insertText: '---\n' },
Expand Down
48 changes: 29 additions & 19 deletions src/renderer/plugins/markdown-code-highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,26 +187,26 @@ function wrap (code: string, lang: string, lineNumber: boolean) {
return html
}

function getLangCodeFromExtension (extension: string) {
const extensionMap: Record<string, string> = {
vue: 'markup',
html: 'markup',
md: 'markdown',
rb: 'ruby',
ts: 'typescript',
py: 'python',
sh: 'bash',
yml: 'yaml',
styl: 'stylus',
kt: 'kotlin',
rs: 'rust',
node: 'js',
asm: 'nasm',
s: 'nasm',
assembly: 'nasm',
masm: 'nasm',
}
const extensionMap: Record<string, string> = {
vue: 'markup',
html: 'markup',
md: 'markdown',
rb: 'ruby',
ts: 'typescript',
py: 'python',
sh: 'bash',
yml: 'yaml',
styl: 'stylus',
kt: 'kotlin',
rs: 'rust',
node: 'js',
asm: 'nasm',
s: 'nasm',
assembly: 'nasm',
masm: 'nasm',
}

function getLangCodeFromExtension (extension: string) {
return extensionMap[extension] || extension
}

Expand Down Expand Up @@ -238,6 +238,16 @@ export default {
md.options.highlight = (str, lang) => highlight(str, lang, true)
})

let supportedLanguages: string

ctx.editor.tapSimpleCompletionItems(items => {
if (!supportedLanguages) {
supportedLanguages = Object.keys(prism.languages).concat(Object.keys(extensionMap)).sort().join(',').replace('DFS,', '')
}

items.push({ label: '/ ``` Code', insertText: '```${1|' + supportedLanguages + '|}\n$2\n```\n' })
})

const exportStyles = `
${defaultStyle}
code[class*="language-"], pre[class*="language-"] {
Expand Down

0 comments on commit de30946

Please sign in to comment.