Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/release-core.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Create Core Release

on:
push:
tags:
- 'core-v*'

jobs:
build:
runs-on: ubuntu-latest
if: github.repository == 'doocs/md'
steps:
- uses: actions/checkout@v5

- uses: actions/setup-node@v5
with:
node-version: 22
registry-url: https://registry.npmjs.org/

- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 10

- run: pnpm install --frozen-lockfile
- run: pnpm core build
- run: echo "registry=https://registry.npmjs.org/" > packages/core/.npmrc

- run: cd packages/core && npm publish --registry=https://registry.npmjs.org/
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
},
"scripts": {
"start": "pnpm web dev",
"core": "pnpm --filter @md/core",
"web": "pnpm --filter @md/web",
"vscode": "pnpm --prefix ./apps/vscode",
"build:cli": "pnpm web build && npx shx rm -rf packages/md-cli/dist && npx shx rm -rf dist/**/*.map && npx shx cp -r apps/web/dist packages/md-cli/ && cd packages/md-cli && npm pack",
"release:cli": "node ./scripts/release.js",
"lint": "eslint . --fix",
"type-check": "vue-tsc --build --force",
"postinstall": "simple-git-hooks",
"postinstall": "simple-git-hooks && pnpm core build",
"inspector": "pnpx node-modules-inspector"
},
"devDependencies": {
Expand Down
37 changes: 37 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
# @md/core

核心渲染引擎,用于将 Markdown 文本渲染为 HTML 内容。

## 使用

```ts
import { initRenderer, renderMarkdown } from '@md/core'

// 初始化 renderer
const renderer = initRenderer({
// options IOpts
})

// 渲染 Markdown
const { html, readingTime } = renderMarkdown(raw, renderer)
```

`IOpts` 参数说明:

```ts
export interface Theme {
base: ExtendedProperties
block: Record<Block, ExtendedProperties>
inline: Record<Inline, ExtendedProperties>
}

export interface IOpts {
theme: Theme
fonts: string
size: string
isUseIndent: boolean
isUseJustify: boolean
legend?: string
citeStatus?: boolean
countStatus?: boolean
isMacCodeBlock?: boolean
isShowLineNumber?: boolean
}
```
36 changes: 30 additions & 6 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,35 @@
"name": "@md/core",
"type": "module",
"version": "2.0.4",
"private": true,
"exports": {
".": "./src/index.ts",
"./renderer": "./src/renderer/index.ts",
"./extensions": "./src/extensions/index.ts",
"./utils": "./src/utils/index.ts"
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./extensions": {
"types": "./dist/extensions/index.d.ts",
"import": "./dist/extensions/index.js",
"require": "./dist/extensions/index.cjs"
},
"./renderer": {
"types": "./dist/renderer/index.d.ts",
"import": "./dist/renderer/index.js",
"require": "./dist/renderer/index.cjs"
},
"./utils": {
"types": "./dist/utils/index.d.ts",
"import": "./dist/utils/index.js",
"require": "./dist/utils/index.cjs"
},
"./package.json": "./package.json"
},
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"dev": "tsdown --watch",
"build": "tsdown"
},
"dependencies": {
"@md/shared": "workspace:*",
Expand All @@ -22,6 +45,7 @@
"reading-time": "^1.5.0"
},
"devDependencies": {
"@md/config": "workspace:*"
"@md/config": "workspace:*",
"tsdown": "^0.15.1"
}
}
4 changes: 2 additions & 2 deletions packages/core/src/extensions/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ const defaultAlertVariant: AlertVariantItem[] = [
* Resolves the variants configuration, combining the provided variants with
* the default variants.
*/
export function resolveVariants(variants: AlertVariantItem[]) {
export function resolveVariants(variants: AlertVariantItem[]): AlertVariantItem[] {
if (!variants.length)
return defaultAlertVariant

Expand All @@ -185,6 +185,6 @@ export function resolveVariants(variants: AlertVariantItem[]) {
/**
* Returns regex pattern to match alert syntax.
*/
export function createSyntaxPattern(type: string) {
export function createSyntaxPattern(type: string): string {
return `^(?:\\[!${type}])\\s*?\n*`
}
2 changes: 1 addition & 1 deletion packages/core/src/extensions/katex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const inlineRuleNonStandard = /^(\${1,2})(?!\$)((?:\\.|[^\\\n])*?(?:\\.|[^\\\n$]

const blockRule = /^\s{0,3}(\${1,2})[ \t]*\n([\s\S]+?)\n\s{0,3}\1[ \t]*(?:\n|$)/

function createRenderer(display: boolean, inlineStyle: string, blockStyle: string) {
function createRenderer(display: boolean, inlineStyle: string, blockStyle: string): (token: any) => string {
return (token: any) => {
// @ts-expect-error MathJax is a global variable
window.MathJax.texReset()
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/basicHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export function escapeHtml(text: string): string {
/**
* 首字母大写
*/
export function ucfirst(str: string) {
export function ucfirst(str: string): string {
return str.slice(0, 1).toUpperCase() + str.slice(1).toLowerCase()
}
2 changes: 1 addition & 1 deletion packages/core/src/utils/markdownHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { marked } from 'marked'
* @param renderer - 渲染器 API
* @returns 渲染结果,包含 HTML 和阅读时间
*/
export function renderMarkdown(raw: string, renderer: RendererAPI) {
export function renderMarkdown(raw: string, renderer: RendererAPI): { html: string, readingTime: ReadTimeResults } {
// 解析 front-matter 和正文
const { markdownContent, readingTime }
= renderer.parseFrontMatterAndContent(raw)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/themeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { PropertiesHyphen } from 'csstype'
export function customizeTheme(theme: Theme, options: {
fontSize?: number
color?: string
}) {
}): Theme {
const newTheme = JSON.parse(JSON.stringify(theme))
const { fontSize, color } = options
if (fontSize) {
Expand All @@ -34,7 +34,7 @@ export function customizeTheme(theme: Theme, options: {
* @param theme - 基础主题
* @returns 合并后的主题
*/
export function customCssWithTemplate(jsonString: Partial<Record<Block | Inline, PropertiesHyphen>>, color: string, theme: Theme) {
export function customCssWithTemplate(jsonString: Partial<Record<Block | Inline, PropertiesHyphen>>, color: string, theme: Theme): Theme {
const newTheme = customizeTheme(theme, { color })

const mergeProperties = <T extends Block | Inline = Block>(target: Record<T, PropertiesHyphen>, source: Partial<Record<Block | Inline, PropertiesHyphen>>, keys: T[]) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"declarationMap": true,
"isolatedDeclarations": true
},
"include": [
"src/**/*"
Expand Down
13 changes: 13 additions & 0 deletions packages/core/tsdown.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from 'tsdown'

export default defineConfig({
entry: {
'index': `./src/index.ts`,
'renderer/index': `./src/renderer/index.ts`,
'extensions/index': `./src/extensions/index.ts`,
'utils/index': `./src/utils/index.ts`,
},
dts: true,
format: [`esm`, `cjs`],
platform: `neutral`,
})
Loading