generated from NetCoreTemplates/blazor-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement markdown comments + Ask Model
- Loading branch information
Showing
13 changed files
with
3,880 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { MarkedMin } from "./marked.min.mjs" | ||
|
||
const marked = new MarkedMin( | ||
markedHighlight({ | ||
langPrefix: 'hljs language-', | ||
highlight(code, lang, info) { | ||
const hljs = globalThis.hljs | ||
const language = hljs.getLanguage(lang) ? lang : 'plaintext' | ||
return hljs.highlight(code, { language }).value | ||
} | ||
}) | ||
) | ||
|
||
export async function renderMarkdown(body) { | ||
const rawHtml = marked.parse(body, { async:false }) | ||
return rawHtml | ||
} | ||
|
||
export function markedHighlight(options) { | ||
if (typeof options === 'function') { | ||
options = { | ||
highlight: options | ||
} | ||
} | ||
|
||
if (!options || typeof options.highlight !== 'function') { | ||
throw new Error('Must provide highlight function') | ||
} | ||
|
||
if (typeof options.langPrefix !== 'string') { | ||
options.langPrefix = 'language-' | ||
} | ||
|
||
return { | ||
async: !!options.async, | ||
walkTokens(token) { | ||
if (token.type !== 'code') { | ||
return | ||
} | ||
|
||
const lang = getLang(token.lang) | ||
|
||
if (options.async) { | ||
return Promise.resolve(options.highlight(token.text, lang, token.lang || '')).then(updateToken(token)) | ||
} | ||
|
||
const code = options.highlight(token.text, lang, token.lang || '') | ||
if (code instanceof Promise) { | ||
throw new Error('markedHighlight is not set to async but the highlight function is async. Set the async option to true on markedHighlight to await the async highlight function.') | ||
} | ||
updateToken(token)(code) | ||
}, | ||
renderer: { | ||
code(code, infoString, escaped) { | ||
const lang = getLang(infoString) | ||
const classAttr = lang | ||
? ` class="${options.langPrefix}${escape(lang)}"` | ||
: ' class="hljs"'; | ||
code = code.replace(/\n$/, '') | ||
return `<pre><code${classAttr}>${escaped ? code : escape(code, true)}\n</code></pre>` | ||
} | ||
} | ||
} | ||
} | ||
|
||
function getLang(lang) { | ||
return (lang || '').match(/\S*/)[0] | ||
} | ||
|
||
function updateToken(token) { | ||
return code => { | ||
if (typeof code === 'string' && code !== token.text) { | ||
token.escaped = true | ||
token.text = code | ||
} | ||
} | ||
} | ||
|
||
// copied from marked helpers | ||
const escapeTest = /[&<>"']/ | ||
const escapeReplace = new RegExp(escapeTest.source, 'g') | ||
const escapeTestNoEncode = /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/ | ||
const escapeReplaceNoEncode = new RegExp(escapeTestNoEncode.source, 'g') | ||
const escapeReplacements = { | ||
'&': '&', | ||
'<': '<', | ||
'>': '>', | ||
'"': '"', | ||
"'": ''' | ||
} | ||
const getEscapeReplacement = ch => escapeReplacements[ch] | ||
function escape(html, encode) { | ||
if (encode) { | ||
if (escapeTest.test(html)) { | ||
return html.replace(escapeReplace, getEscapeReplacement) | ||
} | ||
} else { | ||
if (escapeTestNoEncode.test(html)) { | ||
return html.replace(escapeReplaceNoEncode, getEscapeReplacement) | ||
} | ||
} | ||
|
||
return html | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.