Skip to content

Commit

Permalink
fix: MarkdownItAsync typecheck for markdown-it
Browse files Browse the repository at this point in the history
  • Loading branch information
YunYouJun committed Jan 24, 2025
1 parent cedbb61 commit 7253caa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
10 changes: 6 additions & 4 deletions packages/valaxy/node/plugins/markdown/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { Header } from '@valaxyjs/utils'

import type { MarkdownItAsync } from 'markdown-it-async'
import type { ResolvedValaxyOptions } from '../../options'
import { createMarkdownItAsync } from 'markdown-it-async'

import MarkdownIt from 'markdown-it'
import { highlight } from './plugins/highlight'
import { defaultCodeTheme, setupMarkdownPlugins } from './setup'

Expand All @@ -15,16 +17,16 @@ export interface MarkdownParsedData {
headers?: Header[]
}

export async function createMarkdownRenderer(options?: ResolvedValaxyOptions): Promise<MarkdownIt> {
export async function createMarkdownRenderer(options?: ResolvedValaxyOptions): Promise<MarkdownItAsync> {
const mdOptions = options?.config.markdown || {}
const theme = mdOptions.theme ?? defaultCodeTheme

const md = MarkdownIt({
const md = createMarkdownItAsync({
html: true,
linkify: true,
highlight: await highlight(theme, mdOptions),
...mdOptions.options,
}) as MarkdownIt
})

md.linkify.set({ fuzzyLink: false })

Expand Down
14 changes: 8 additions & 6 deletions packages/valaxy/node/plugins/markdown/setup.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
import type MarkdownIt from 'markdown-it'

import type { MarkdownItAsync } from 'markdown-it-async'
import type Token from 'markdown-it/lib/token.mjs'
import type { ResolvedValaxyOptions } from '../../options'

import type { ResolvedValaxyOptions } from '../../options'
import type { ThemeOptions } from './types'
import {
headersPlugin,
type HeadersPluginOptions,
} from '@mdit-vue/plugin-headers'

import { sfcPlugin, type SfcPluginOptions } from '@mdit-vue/plugin-sfc'

import { titlePlugin } from '@mdit-vue/plugin-title'

import { tocPlugin, type TocPluginOptions } from '@mdit-vue/plugin-toc'
import { slugify } from '@mdit-vue/shared'
import { cssI18nContainer } from 'css-i18n'
import anchorPlugin from 'markdown-it-anchor'

import anchorPlugin from 'markdown-it-anchor'
import attrsPlugin from 'markdown-it-attrs'
import { full as emojiPlugin } from 'markdown-it-emoji'

import { full as emojiPlugin } from 'markdown-it-emoji'
import footnotePlugin from 'markdown-it-footnote'
// https://www.npmjs.com/package/markdown-it-image-figures
import imageFigures from 'markdown-it-image-figures'
import TaskLists from 'markdown-it-task-lists'
import { linkPlugin } from './plugins/link'
import { containerPlugin } from './plugins/markdown-it/container'

import { containerPlugin } from './plugins/markdown-it/container'
import { footnoteTooltipPlugin } from './plugins/markdown-it/footnoteTooltip'
import { highlightLinePlugin } from './plugins/markdown-it/highlightLines'
import Katex from './plugins/markdown-it/katex'
import { lineNumberPlugin } from './plugins/markdown-it/lineNumbers'
import { preWrapperPlugin } from './plugins/markdown-it/preWrapper'

import { snippetPlugin } from './plugins/markdown-it/snippet'

export const defaultCodeTheme = { light: 'github-light', dark: 'github-dark' } as const as ThemeOptions

export async function setupMarkdownPlugins(
md: MarkdownIt,
md: MarkdownItAsync,
options?: ResolvedValaxyOptions,
// isExcerpt = false,
base = '/',
Expand Down
9 changes: 5 additions & 4 deletions packages/valaxy/node/plugins/markdown/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import type {

import type { SfcPluginOptions } from '@mdit-vue/plugin-sfc'
import type { TocPluginOptions } from '@mdit-vue/plugin-toc'

import type { KatexOptions } from 'katex'
import type MarkdownIt from 'markdown-it'

import type MarkdownIt from 'markdown-it'
import type anchorPlugin from 'markdown-it-anchor'

import type { MarkdownItAsync } from 'markdown-it-async'
import type {
BuiltinTheme,
Highlighter,
Expand Down Expand Up @@ -39,15 +40,15 @@ export interface MarkdownOptions {
/**
* Setup markdown-it instance before applying plugins
*/
preConfig?: (md: MarkdownIt) => void
preConfig?: (md: MarkdownItAsync) => void
/**
* markdown-it options
*/
options?: MarkdownIt['options']
/**
* config markdown-it
*/
config?: (md: MarkdownIt) => void
config?: (md: MarkdownItAsync) => void
anchor?: anchorPlugin.AnchorOptions
attrs?: {
leftDelimiter?: string
Expand Down
6 changes: 3 additions & 3 deletions packages/valaxy/node/plugins/vueRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ValaxyNode } from '../types'
import fs from 'fs-extra'
import matter from 'gray-matter'
import { convert } from 'html-to-text'
import MarkdownIt from 'markdown-it'
import { MarkdownItAsync } from 'markdown-it-async'
import { resolve } from 'pathe'
import VueRouter from 'unplugin-vue-router/vite'

Expand All @@ -17,7 +17,7 @@ import { presetStatistics } from './presets/statistics'
* @param excerpt
* @param type
*/
function getExcerptByType(excerpt = '', type: ExcerptType = 'html', mdIt: MarkdownIt) {
function getExcerptByType(excerpt = '', type: ExcerptType = 'html', mdIt: MarkdownItAsync) {
switch (type) {
case 'ai':
case 'md':
Expand All @@ -39,7 +39,7 @@ export async function createRouterPlugin(valaxyApp: ValaxyNode) {
const { options } = valaxyApp
const { roots, config: valaxyConfig } = options

const mdIt = new MarkdownIt({ html: true })
const mdIt = new MarkdownItAsync({ html: true })
await setupMarkdownPlugins(mdIt, options)

return VueRouter({
Expand Down

0 comments on commit 7253caa

Please sign in to comment.