diff options
author | Julius Härtl <jus@bitgrid.net> | 2019-07-02 15:18:50 +0300 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2019-07-02 15:18:50 +0300 |
commit | 46e60926dd5a7d1803bddfcf655b2220a5b8d3a8 (patch) | |
tree | 4cf252462061e8934167f781180fc1b20f3ae032 | |
parent | 2ffdbe9cfb13419bb189497392dd8255e6a6a1fc (diff) |
Dynamically load syntax highlighting
Signed-off-by: Julius Härtl <jus@bitgrid.net>
-rw-r--r-- | src/EditorFactory.js | 4 | ||||
-rw-r--r-- | src/components/EditorWrapper.vue | 8 | ||||
-rw-r--r-- | src/components/FilesEditor.vue | 4 | ||||
-rw-r--r-- | src/helpers/mappings.js | 97 |
4 files changed, 108 insertions, 5 deletions
diff --git a/src/EditorFactory.js b/src/EditorFactory.js index adbafbaa1..c03742123 100644 --- a/src/EditorFactory.js +++ b/src/EditorFactory.js @@ -40,7 +40,7 @@ import MarkdownIt from 'markdown-it' import { MarkdownSerializer, defaultMarkdownSerializer } from 'prosemirror-markdown' -const loadSyntaxHighlight = async (languages) => { +const loadSyntaxHighlight = async(languages) => { let modules = {} for (let i = 0; i < languages.length; i++) { const lang = await import('highlight.js/lib/languages/' + languages[i]) @@ -52,7 +52,7 @@ const loadSyntaxHighlight = async (languages) => { return { languages: modules } } -const createEditor = async ({ content, onUpdate, extensions, enableRichEditing, languages }) => { +const createEditor = async({ content, onUpdate, extensions, enableRichEditing, languages }) => { const highlight = await loadSyntaxHighlight(languages) let richEditingExtensions = [] if (enableRichEditing) { diff --git a/src/components/EditorWrapper.vue b/src/components/EditorWrapper.vue index 7f4235e6f..9f082668a 100644 --- a/src/components/EditorWrapper.vue +++ b/src/components/EditorWrapper.vue @@ -55,6 +55,7 @@ import Vue from 'vue' import { SyncService, ERROR_TYPE } from './../services/SyncService' import { endpointUrl, getRandomGuestName } from './../helpers' +import { extensionHighlight } from '../helpers/mappings' import { createEditor, markdownit, createMarkdownSerializer, serializePlainText } from './../EditorFactory' import { EditorContent } from 'tiptap' @@ -176,6 +177,9 @@ export default { }, isRichEditor() { return this.mime === 'text/markdown' + }, + fileExtension() { + return this.relativePath.split('/').pop().split('.').pop() } }, watch: { @@ -279,7 +283,9 @@ export default { }) ], enableRichEditing: this.isRichEditor, - languages: ['cpp', 'css', 'php'] + languages: [ + extensionHighlight[this.fileExtension] ? extensionHighlight[this.fileExtension] : this.fileExtension + ] }).then(editor => { this.tiptap = editor this.syncService.state = this.tiptap.state diff --git a/src/components/FilesEditor.vue b/src/components/FilesEditor.vue index d03a85dae..a21893a90 100644 --- a/src/components/FilesEditor.vue +++ b/src/components/FilesEditor.vue @@ -21,7 +21,7 @@ --> <template> - <editor-wrapper :file-id="fileId" :relative-path="relativePath" :active="active" + <editor-wrapper :file-id="fileId" :relative-path="davPath" :active="active" :share-token="shareToken" :mime="mime" /> </template> @@ -32,7 +32,7 @@ export default { EditorWrapper: () => import('./EditorWrapper') }, props: { - relativePath: { + davPath: { type: String, default: null }, diff --git a/src/helpers/mappings.js b/src/helpers/mappings.js new file mode 100644 index 000000000..4b859fae4 --- /dev/null +++ b/src/helpers/mappings.js @@ -0,0 +1,97 @@ +/* + * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +const extensionHighlight = { + py: 'python', + gyp: 'python', + wsgi: 'python', + htm: 'html', + xhtml: 'html', + erl: 'erlang', + jsp: 'java', + pl: 'perl', + rss: 'xml', + atom: 'xml', + xsl: 'xml', + plist: 'xml', + rb: 'ruby', + builder: 'ruby', + gemspec: 'ruby', + podspec: 'ruby', + thor: 'ruby', + diff: 'patch', + hs: 'haskell', + icl: 'haskell', + php3: 'php', + php4: 'php', + php5: 'php', + php6: 'php', + sh: 'bash', + zsh: 'bash', + st: 'smalltalk', + as: 'actionscript', + apacheconf: 'apache', + osacript: 'applescript', + b: 'brainfuck', + bf: 'brainfuck', + clj: 'clojure', + 'cmake.in': 'cmake', + coffee: 'coffeescript', + cson: 'coffescript', + iced: 'coffescript', + c: 'cpp', + 'c++': 'cpp', + 'h++': 'cpp', + hh: 'cpp', + jinja: 'django', + bat: 'dos', + cmd: 'dos', + fs: 'fsharp', + hbs: 'handlebars', + 'html.hbs': 'handlebars', + 'html.handlebars': 'handlebars', + 'sublime_metrics': 'json', + 'sublime_session': 'json', + 'sublime-keymap': 'json', + 'sublime-mousemap': 'json', + 'sublime-project': 'json', + 'sublime-settings': 'json', + 'sublime-workspace': 'json', + mk: 'makefile', + mak: 'makefile', + md: 'markdown', + mkdown: 'markdown', + mkd: 'markdown', + nginxconf: 'nginx', + m: 'objectivec', + mm: 'objectivec', + ml: 'ocaml', + rs: 'rust', + sci: 'scilab', + vb: 'vbnet', + vbs: 'vbscript' +} + +export default extensionHighlight +export { + extensionHighlight +} |