Skip to content

Commit

Permalink
refactor(ui/marked): replace pkg-level marked with new class instance (
Browse files Browse the repository at this point in the history
…#954)

* refactor(ui/marked): replace pkg-level marked with new class instance

* fix sidebar
  • Loading branch information
qwqcode authored Aug 18, 2024
1 parent b97167a commit a925806
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
26 changes: 13 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ui/artalk-sidebar/src/components/Tab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ router.beforeEach((to, from) => {
})
router.afterEach((to, from, failure) => {
curtPage.value = to.name.replace(/^\//, '')
curtPage.value = String(to.name).replace(/^\//, '')
})
function showSearch() {
Expand Down
7 changes: 6 additions & 1 deletion ui/artalk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@
},
"devDependencies": {
"hanabi": "^0.4.0",
"insane": "^2.6.2",
"insane": "^2.6.2"
},
"dependencies": {
"marked": "^14.0.0"
},
"peerDependencies": {
"marked": "^14.0.0"
}
}
12 changes: 7 additions & 5 deletions ui/artalk/src/lib/marked.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { marked as libMarked, MarkedOptions } from 'marked'
import { Marked } from 'marked'
import type { MarkedOptions } from 'marked'

import { sanitize } from './sanitizer'
import { renderCode } from './highlight'
Expand All @@ -7,7 +8,7 @@ import type { ArtalkConfig } from '@/types'

type Replacer = (raw: string) => string

let instance: typeof libMarked | undefined
let instance: Marked | undefined
let replacers: Replacer[] = []

const markedOptions: MarkedOptions = {
Expand All @@ -33,21 +34,22 @@ export interface MarkedInitOptions {
/** 初始化 marked */
export function initMarked(options: MarkedInitOptions) {
try {
if (!libMarked.name) return
if (!Marked.name) return
} catch {
return
}

// @see https://github.com/markedjs/marked/blob/4afb228d956a415624c4e5554bb8f25d047676fe/src/Tokenizer.js#L329
libMarked.setOptions({
const marked = new Marked()
marked.setOptions({
renderer: getRenderer({
imgLazyLoad: options.imgLazyLoad,
}),
...markedOptions,
...options.markedOptions,
})

instance = libMarked
instance = marked
}

/** 解析 markdown */
Expand Down
3 changes: 2 additions & 1 deletion ui/artalk/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MarkedOptions } from 'marked'
import type { CommentData } from './data'
import type { EditorApi } from './editor'
import type { I18n } from '@/i18n'
Expand Down Expand Up @@ -133,7 +134,7 @@ export interface ArtalkConfig {
markedReplacers?: ((raw: string) => string)[]

/** Marked options */
markedOptions?: any // MarkedOptions
markedOptions?: MarkedOptions

/** 列表请求参数修改器 */
listFetchParamsModifier?: (params: any) => void
Expand Down
3 changes: 2 additions & 1 deletion ui/artalk/src/types/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Marked } from 'marked'
import type {
SidebarShowPayload,
EventPayloadMap,
Expand Down Expand Up @@ -32,7 +33,7 @@ export interface ContextApi extends EventManagerFuncs<EventPayloadMap> {
conf: ArtalkConfig

/** marked 依赖对象 */
getMarked(): any | undefined
getMarked(): Marked | undefined

/** 获取 API 以供 HTTP 请求 */
getApi(): Api
Expand Down

0 comments on commit a925806

Please sign in to comment.