From 01e255d826263f2da651d8cbffdc8b36f816de30 Mon Sep 17 00:00:00 2001 From: dribble-njr Date: Wed, 20 Nov 2024 22:13:35 +0800 Subject: [PATCH] feat: offsetting the default margin of p --- src/assets/example/theme-css.txt | 3 +++ src/config/theme.ts | 5 +++++ src/utils/MDAlert.ts | 10 +++++++--- src/utils/index.ts | 11 ++++++++++- src/utils/renderer.ts | 5 ++--- 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/assets/example/theme-css.txt b/src/assets/example/theme-css.txt index 230082f..9c04218 100644 --- a/src/assets/example/theme-css.txt +++ b/src/assets/example/theme-css.txt @@ -39,6 +39,9 @@ markdown-alert { /* GFM 警告块标题 */ markdown-alert-title { } +/* GFM 警告块内容,抵消 p 默认的 margin */ +markdown-alert-content-wrapper { +} /* GFM note */ markdown-alert-title-note { } diff --git a/src/config/theme.ts b/src/config/theme.ts index e4e3d0c..5de9a61 100644 --- a/src/config/theme.ts +++ b/src/config/theme.ts @@ -111,6 +111,11 @@ const defaultTheme: Theme = { 'align-items': `center`, }, + // GFM 警告块内容,抵消 p 默认的 margin + 'markdown-alert-content-wrapper': { + margin: `-1em -8px -1.5em;`, + }, + 'markdown-alert-title-note': { color: `#478be6`, }, diff --git a/src/utils/MDAlert.ts b/src/utils/MDAlert.ts index 6b0bca1..9028d2e 100644 --- a/src/utils/MDAlert.ts +++ b/src/utils/MDAlert.ts @@ -1,5 +1,6 @@ import type { AlertOptions, AlertVariantItem } from '@/types' import type { MarkedExtension, Tokens } from 'marked' +import { getStyleString } from '.' /** * https://github.com/bent10/marked-extensions/tree/main/packages/alert @@ -44,6 +45,9 @@ export default function markedAlert(options: AlertOptions = {}): MarkedExtension ...options.theme?.block[titleClassName], ...options.theme?.block[`${titleClassName}-${variantType}`], }, + contentWrapperStyle: { + margin: options.theme?.block[`${className}-content-wrapper`]?.margin, + }, }, }) @@ -77,15 +81,15 @@ export default function markedAlert(options: AlertOptions = {}): MarkedExtension name: `alert`, level: `block`, renderer({ meta, tokens = [] }) { - let tmpl = `
\n` - tmpl += `

` + let tmpl = `

\n` + tmpl += `

` tmpl += meta.icon.replace( `\n` - tmpl += this.parser.parse(tokens) + tmpl += `

${this.parser.parse(tokens)}
` tmpl += `
\n` return tmpl diff --git a/src/utils/index.ts b/src/utils/index.ts index c787cfa..a18c86c 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,4 +1,4 @@ -import type { Block, Inline, Theme } from '@/types' +import type { Block, ExtendedProperties, Inline, Theme } from '@/types' import type { PropertiesHyphen } from 'csstype' import { prefix } from '@/config' @@ -116,6 +116,15 @@ export function css2json(css: string): Partial `${key}: ${value}`).join(`; `) +} + /** * 格式化内容 * @param {string} content - 要格式化的内容 diff --git a/src/utils/renderer.ts b/src/utils/renderer.ts index ad9d991..ca43515 100644 --- a/src/utils/renderer.ts +++ b/src/utils/renderer.ts @@ -6,6 +6,7 @@ import hljs from 'highlight.js' import { marked } from 'marked' import mermaid from 'mermaid' +import { getStyleString } from '.' import markedAlert from './MDAlert' import { MDKatex } from './MDKatex' @@ -59,9 +60,7 @@ function getStyles(styleMapping: ThemeStyles, tokenName: string, addition: strin if (!dict) { return `` } - const styles = Object.entries(dict) - .map(([key, value]) => `${key}:${value}`) - .join(`;`) + const styles = getStyleString(dict) return `style="${styles}${addition}"` }