mirror of
https://github.com/marktext/marktext.git
synced 2025-05-03 01:40:13 +08:00
fix: code highlight error when open file contains code block (#891)
* fix: code highlight error when open file contains code block * some typo error * some style change
This commit is contained in:
parent
749af381f1
commit
c0f333d9a2
@ -103,6 +103,7 @@ export default function renderContainerBlock (block, cursor, activeBlocks, selec
|
|||||||
Object.assign(data.dataset, { role: functionType })
|
Object.assign(data.dataset, { role: functionType })
|
||||||
selector += PRE_BLOCK_HASH[block.functionType]
|
selector += PRE_BLOCK_HASH[block.functionType]
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!block.parent) {
|
if (!block.parent) {
|
||||||
return h(selector, data, [this.renderIcon(block), ...block.children.map(child => this.renderBlock(child, cursor, activeBlocks, selectedBlock, matches, useCache))])
|
return h(selector, data, [this.renderIcon(block), ...block.children.map(child => this.renderBlock(child, cursor, activeBlocks, selectedBlock, matches, useCache))])
|
||||||
} else {
|
} else {
|
||||||
|
@ -27,7 +27,7 @@ export default function renderIcon (block) {
|
|||||||
console.error('Only top most block can render front icon button.')
|
console.error('Only top most block can render front icon button.')
|
||||||
}
|
}
|
||||||
const { type, functionType, listType } = block
|
const { type, functionType, listType } = block
|
||||||
const selector = `span.${CLASS_OR_ID['AG_FRONT_ICON']}`
|
const selector = `a.${CLASS_OR_ID['AG_FRONT_ICON']}`
|
||||||
let icon = null
|
let icon = null
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
@ -32,7 +32,7 @@ function getPeerDependents (mainLanguage) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function initLoadLanguage (Prism) {
|
function initLoadLanguage (Prism) {
|
||||||
return function loadLanguages (arr, withoutDependencies) {
|
return async function loadLanguages (arr, withoutDependencies) {
|
||||||
// If no argument is passed, load all components
|
// If no argument is passed, load all components
|
||||||
if (!arr) {
|
if (!arr) {
|
||||||
arr = Object.keys(languages).filter(function (language) {
|
arr = Object.keys(languages).filter(function (language) {
|
||||||
@ -40,32 +40,46 @@ function initLoadLanguage (Prism) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
if (arr && !arr.length) {
|
if (arr && !arr.length) {
|
||||||
return
|
return Promise.reject('The first parameter should be a list of load languages or single language.')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Array.isArray(arr)) {
|
if (!Array.isArray(arr)) {
|
||||||
arr = [arr]
|
arr = [arr]
|
||||||
}
|
}
|
||||||
|
|
||||||
arr.forEach(function (language) {
|
const promises = []
|
||||||
|
|
||||||
|
for (const language of arr) {
|
||||||
|
// handle not existed
|
||||||
if (!languages[language]) {
|
if (!languages[language]) {
|
||||||
console.warn('Language does not exist ' + language)
|
promises.push(Promise.resolve({
|
||||||
return
|
lang: language,
|
||||||
|
status: 'noexist'
|
||||||
|
}))
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
// handle already cached
|
||||||
if (loadedCache.has(language)) {
|
if (loadedCache.has(language)) {
|
||||||
return
|
promises.push(Promise.resolve({
|
||||||
|
lang: language,
|
||||||
|
status: 'cached'
|
||||||
|
}))
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load dependencies first
|
// Load dependencies first
|
||||||
if (!withoutDependencies && languages[language].require) {
|
if (!withoutDependencies && languages[language].require) {
|
||||||
loadLanguages(languages[language].require)
|
const results = await loadLanguages(languages[language].require)
|
||||||
|
promises.push(...results)
|
||||||
}
|
}
|
||||||
|
|
||||||
delete Prism.languages[language]
|
delete Prism.languages[language]
|
||||||
import('prismjs2/components/prism-' + language)
|
await import('prismjs2/components/prism-' + language)
|
||||||
.then(_ => {
|
|
||||||
loadedCache.add(language)
|
loadedCache.add(language)
|
||||||
})
|
promises.push(Promise.resolve({
|
||||||
|
status: 'loaded',
|
||||||
|
lang: language
|
||||||
|
}))
|
||||||
|
|
||||||
// Reload dependents
|
// Reload dependents
|
||||||
const dependents = getPeerDependents(language).filter(function (dependent) {
|
const dependents = getPeerDependents(language).filter(function (dependent) {
|
||||||
@ -78,9 +92,12 @@ function initLoadLanguage (Prism) {
|
|||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
if (dependents.length) {
|
if (dependents.length) {
|
||||||
loadLanguages(dependents, true)
|
const results = await loadLanguages(dependents, true)
|
||||||
|
promises.push(...results)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
return Promise.all(promises)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -441,6 +441,7 @@ class Selection {
|
|||||||
let { anchorNode, anchorOffset, focusNode, focusOffset } = this.doc.getSelection()
|
let { anchorNode, anchorOffset, focusNode, focusOffset } = this.doc.getSelection()
|
||||||
let startParagraph = findNearestParagraph(anchorNode)
|
let startParagraph = findNearestParagraph(anchorNode)
|
||||||
let endParagraph = findNearestParagraph(focusNode)
|
let endParagraph = findNearestParagraph(focusNode)
|
||||||
|
|
||||||
if (!startParagraph || !endParagraph) {
|
if (!startParagraph || !endParagraph) {
|
||||||
return {
|
return {
|
||||||
start: null,
|
start: null,
|
||||||
|
@ -104,6 +104,19 @@ const importRegister = ContentState => {
|
|||||||
const inputBlock = this.createBlock('span', lang)
|
const inputBlock = this.createBlock('span', lang)
|
||||||
if (lang) {
|
if (lang) {
|
||||||
loadLanguage(lang)
|
loadLanguage(lang)
|
||||||
|
.then(infoList => {
|
||||||
|
if (!Array.isArray(infoList)) return
|
||||||
|
// There are three status `loaded`, `noexist` and `cached`.
|
||||||
|
// if the status is `loaded`, indicated that it's a new loaded language
|
||||||
|
const needRender = infoList.some(({ status }) => status === 'loaded')
|
||||||
|
if (needRender) {
|
||||||
|
this.render()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
// if no parameter provided, will cause error.
|
||||||
|
console.warn(err)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
inputBlock.functionType = 'languageInput'
|
inputBlock.functionType = 'languageInput'
|
||||||
this.codeBlocks.set(block.key, value)
|
this.codeBlocks.set(block.key, value)
|
||||||
|
@ -127,10 +127,10 @@ kbd {
|
|||||||
|
|
||||||
#ag-editor-id {
|
#ag-editor-id {
|
||||||
max-width: var(--editorAreaWidth);
|
max-width: var(--editorAreaWidth);
|
||||||
|
min-height: 100%;
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
padding: 20px 50px 40px 50px;
|
padding: 20px 50px 100px 50px;
|
||||||
padding-top: 20px;
|
box-sizing: border-box;
|
||||||
padding-bottom: 100px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ag-editor-id, [contenteditable] {
|
#ag-editor-id, [contenteditable] {
|
||||||
|
Loading…
Reference in New Issue
Block a user