Skip to content

Commit 1ac5834

Browse files
committed
refactor(Editor): load syntax highlighting in editor factory
Signed-off-by: Max <[email protected]>
1 parent fae0ddf commit 1ac5834

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

src/EditorFactory.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,11 @@ import { lowlight } from 'lowlight/lib/core.js'
2727
import hljs from 'highlight.js/lib/core'
2828

2929
import { logger } from './helpers/logger.js'
30+
import { extensionHighlight } from './helpers/mappings.js'
3031
import { FocusTrap, Mention, PlainText, RichText } from './extensions/index.js'
3132
import { PlainTextLowlight } from './nodes/PlainTextLowlight.js'
3233

33-
export const loadSyntaxHighlight = async (language) => {
34-
const list = hljs.listLanguages()
35-
logger.debug('Supported languages', { list })
36-
if (!lowlight.listLanguages().includes(language)) {
37-
try {
38-
logger.debug('Loading language', language)
39-
// eslint-disable-next-line n/no-missing-import
40-
const syntax = await import(/* webpackChunkName: "highlight/[request]" */`../node_modules/highlight.js/lib/languages/${language}.js`)
41-
lowlight.registerLanguage(language, syntax.default)
42-
} catch (error) {
43-
// fallback to none
44-
logger.debug('No matching highlighing found', { error })
45-
}
46-
}
47-
}
48-
49-
export const createRichEditor = ({ extensions = [], session, relativePath, isEmbedded = false } = {}) => {
34+
export const createRichEditor = async ({ extensions = [], session, relativePath, isEmbedded = false } = {}) => {
5035
return _createEditor([
5136
FocusTrap,
5237
RichText.configure({
@@ -64,7 +49,14 @@ export const createRichEditor = ({ extensions = [], session, relativePath, isEmb
6449
])
6550
}
6651

67-
export const createPlainEditor = ({ language, extensions = [] } = {}) => {
52+
export const createPlainEditor = async ({ relativePath, extensions = [] } = {}) => {
53+
54+
const fileExtension = relativePath
55+
? relativePath.split('/').pop().split('.').pop()
56+
: 'txt'
57+
const language = extensionHighlight[fileExtension] || fileExtension
58+
await loadSyntaxHighlight(language)
59+
6860
return _createEditor([
6961
PlainText,
7062
PlainTextLowlight
@@ -79,3 +71,20 @@ const _createEditor = extensions => {
7971
extensions,
8072
})
8173
}
74+
75+
const loadSyntaxHighlight = async language => {
76+
77+
const list = hljs.listLanguages()
78+
logger.debug('Supported languages', { list })
79+
if (!lowlight.listLanguages().includes(language)) {
80+
try {
81+
logger.debug('Loading language', language)
82+
// eslint-disable-next-line n/no-missing-import
83+
const syntax = await import(/* webpackChunkName: "highlight/[request]" */`../node_modules/highlight.js/lib/languages/${language}.js`)
84+
lowlight.registerLanguage(language, syntax.default)
85+
} catch (error) {
86+
// fallback to none
87+
logger.debug('No matching highlighing found', { error })
88+
}
89+
}
90+
}

src/components/Editor.vue

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,9 @@ import { getDocumentState, applyDocumentState, getUpdateMessage } from '../helpe
113113
import { SyncService, ERROR_TYPE, IDLE_TIMEOUT } from './../services/SyncService.js'
114114
import createSyncServiceProvider from './../services/SyncServiceProvider.js'
115115
import AttachmentResolver from './../services/AttachmentResolver.js'
116-
import { extensionHighlight } from '../helpers/mappings.js'
117116
import {
118117
createRichEditor,
119118
createPlainEditor,
120-
loadSyntaxHighlight,
121119
} from './../EditorFactory.js'
122120
import { serializeEditorContent } from './../extensions/Serializer.js'
123121
import markdownit from './../markdownit/index.js'
@@ -285,9 +283,6 @@ export default {
285283
isRichEditor() {
286284
return loadState('text', 'rich_editing_enabled', true) && this.mime === 'text/markdown'
287285
},
288-
fileExtension() {
289-
return this.relativePath ? this.relativePath.split('/').pop().split('.').pop() : 'txt'
290-
},
291286
currentDirectory() {
292287
return this.relativePath
293288
? this.relativePath.split('/').slice(0, -1).join('/')
@@ -522,7 +517,7 @@ export default {
522517
})
523518
},
524519
525-
async createEditor() {
520+
createEditor() {
526521
const session = this.currentSession
527522
528523
const extensions = [
@@ -544,8 +539,6 @@ export default {
544539
}),
545540
]
546541
547-
const language = extensionHighlight[this.fileExtension] || this.fileExtension
548-
549542
if (this.isRichEditor) {
550543
return createRichEditor({
551544
relativePath: this.relativePath,
@@ -554,8 +547,10 @@ export default {
554547
isEmbedded: this.isEmbedded,
555548
})
556549
} else {
557-
await loadSyntaxHighlight(language)
558-
return createPlainEditor({ language, extensions })
550+
return createPlainEditor({
551+
relativePath: this.relativePath,
552+
extensions,
553+
})
559554
}
560555
},
561556

0 commit comments

Comments
 (0)