From 1fa1df89dd812cddbc71cb6e802b83feae3c0803 Mon Sep 17 00:00:00 2001 From: koy Date: Tue, 30 Jul 2024 19:30:02 +0800 Subject: [PATCH 1/4] chore: polish code --- src/core/render/compiler.js | 94 ++++------------------------ src/core/render/compiler/headling.js | 31 +++++++++ src/core/render/compiler/media.js | 42 +++++++++++++ src/core/render/index.js | 17 ++--- src/core/router/history/hash.js | 2 +- src/core/util/dom.js | 13 ++++ 6 files changed, 104 insertions(+), 95 deletions(-) create mode 100644 src/core/render/compiler/headling.js create mode 100644 src/core/render/compiler/media.js diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index d01e65aea..76266a2f1 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -5,63 +5,18 @@ import { tree as treeTpl } from './tpl.js'; import { genTree } from './gen-tree.js'; import { slugify } from './slugify.js'; import { emojify } from './emojify.js'; -import { - getAndRemoveConfig, - removeAtag, - getAndRemoveDocsifyIgnoreConfig, -} from './utils.js'; +import { getAndRemoveConfig } from './utils.js'; import { imageCompiler } from './compiler/image.js'; +import { headingCompiler } from './compiler/headling.js'; import { highlightCodeCompiler } from './compiler/code.js'; import { paragraphCompiler } from './compiler/paragraph.js'; import { taskListCompiler } from './compiler/taskList.js'; import { taskListItemCompiler } from './compiler/taskListItem.js'; import { linkCompiler } from './compiler/link.js'; +import { compileMedia } from './compiler/media.js'; const cachedLinks = {}; -const compileMedia = { - markdown(url) { - return { - url, - }; - }, - mermaid(url) { - return { - url, - }; - }, - iframe(url, title) { - return { - html: ``, - }; - }, - video(url, title) { - return { - html: ``, - }; - }, - audio(url, title) { - return { - html: ``, - }; - }, - code(url, title) { - let lang = url.match(/\.(\w+)$/); - - lang = title || (lang && lang[1]); - if (lang === 'md') { - lang = 'markdown'; - } - - return { - url, - lang, - }; - }, -}; - export class Compiler { constructor(config, router) { this.config = config; @@ -173,7 +128,7 @@ export class Compiler { type = 'audio'; } - embed = compileMedia[type].call(this, href, title); + embed = compileMedia[type](href, title); embed.type = type; } @@ -198,47 +153,22 @@ export class Compiler { _initRenderer() { const renderer = new marked.Renderer(); const { linkTarget, linkRel, router, contentBase } = this; - const _self = this; + // Supports mermaid const origin = {}; - /** - * Render anchor tag - * @link https://github.com/markedjs/marked#overriding-renderer-methods - * @param {String} tokens the content tokens - * @param {Number} depth Type of heading (h tag) - * @returns {String} Heading element - */ - origin.heading = renderer.heading = function ({ tokens, depth }) { - const text = this.parser.parseInline(tokens); - let { str, config } = getAndRemoveConfig(text); - const nextToc = { depth, title: str }; - - const { content, ignoreAllSubs, ignoreSubHeading } = - getAndRemoveDocsifyIgnoreConfig(str); - str = content.trim(); - - nextToc.title = removeAtag(str); - nextToc.ignoreAllSubs = ignoreAllSubs; - nextToc.ignoreSubHeading = ignoreSubHeading; - const slug = slugify(config.id || str); - const url = router.toURL(router.getCurrentPath(), { id: slug }); - nextToc.slug = url; - _self.toc.push(nextToc); - - // Note: tabindex="-1" allows programmatically focusing on heading - // elements after navigation. This is preferred over focusing on the link - // within the heading because it matches the focus behavior of screen - // readers when navigating page content. - return `${str}`; - }; - + // renderer customizers + origin.heading = headingCompiler({ + renderer, + router, + compilerClass: this, + }); origin.code = highlightCodeCompiler({ renderer }); origin.link = linkCompiler({ renderer, router, linkTarget, linkRel, - compilerClass: _self, + compilerClass: this, }); origin.paragraph = paragraphCompiler({ renderer }); origin.image = imageCompiler({ renderer, contentBase, router }); diff --git a/src/core/render/compiler/headling.js b/src/core/render/compiler/headling.js new file mode 100644 index 000000000..c32b9044d --- /dev/null +++ b/src/core/render/compiler/headling.js @@ -0,0 +1,31 @@ +import { + getAndRemoveConfig, + removeAtag, + getAndRemoveDocsifyIgnoreConfig, +} from '../utils.js'; +import { slugify } from './../slugify.js'; + +export const headingCompiler = ({ renderer, router, compilerClass }) => + (renderer.heading = function ({ tokens, depth }) { + const text = this.parser.parseInline(tokens); + let { str, config } = getAndRemoveConfig(text); + const nextToc = { depth, title: str }; + + const { content, ignoreAllSubs, ignoreSubHeading } = + getAndRemoveDocsifyIgnoreConfig(str); + str = content.trim(); + + nextToc.title = removeAtag(str); + nextToc.ignoreAllSubs = ignoreAllSubs; + nextToc.ignoreSubHeading = ignoreSubHeading; + const slug = slugify(config.id || str); + const url = router.toURL(router.getCurrentPath(), { id: slug }); + nextToc.slug = url; + compilerClass.toc.push(nextToc); + + // Note: tabindex="-1" allows programmatically focusing on heading + // elements after navigation. This is preferred over focusing on the link + // within the heading because it matches the focus behavior of screen + // readers when navigating page content. + return `${str}`; + }); diff --git a/src/core/render/compiler/media.js b/src/core/render/compiler/media.js new file mode 100644 index 000000000..d12b1fb0d --- /dev/null +++ b/src/core/render/compiler/media.js @@ -0,0 +1,42 @@ +export const compileMedia = { + markdown(url) { + return { + url, + }; + }, + mermaid(url) { + return { + url, + }; + }, + iframe(url, title) { + return { + html: ``, + }; + }, + video(url, title) { + return { + html: ``, + }; + }, + audio(url, title) { + return { + html: ``, + }; + }, + code(url, title) { + let lang = url.match(/\.(\w+)$/); + + lang = title || (lang && lang[1]); + if (lang === 'md') { + lang = 'markdown'; + } + + return { + url, + lang, + }; + }, +}; diff --git a/src/core/render/index.js b/src/core/render/index.js index 06f900bb7..e85a841c2 100644 --- a/src/core/render/index.js +++ b/src/core/render/index.js @@ -82,7 +82,7 @@ export function Render(Base) { } } - this._renderTo(markdownElm, html); + dom.renderTo(markdownElm, html); // Execute markdown