feat: offsetting the default margin of p

This commit is contained in:
dribble-njr 2024-11-20 22:13:35 +08:00
parent 8dcc2c2756
commit 01e255d826
5 changed files with 27 additions and 7 deletions

View File

@ -39,6 +39,9 @@ markdown-alert {
/* GFM 警告块标题 */ /* GFM 警告块标题 */
markdown-alert-title { markdown-alert-title {
} }
/* GFM 警告块内容,抵消 p 默认的 margin */
markdown-alert-content-wrapper {
}
/* GFM note */ /* GFM note */
markdown-alert-title-note { markdown-alert-title-note {
} }

View File

@ -111,6 +111,11 @@ const defaultTheme: Theme = {
'align-items': `center`, 'align-items': `center`,
}, },
// GFM 警告块内容,抵消 p 默认的 margin
'markdown-alert-content-wrapper': {
margin: `-1em -8px -1.5em;`,
},
'markdown-alert-title-note': { 'markdown-alert-title-note': {
color: `#478be6`, color: `#478be6`,
}, },

View File

@ -1,5 +1,6 @@
import type { AlertOptions, AlertVariantItem } from '@/types' import type { AlertOptions, AlertVariantItem } from '@/types'
import type { MarkedExtension, Tokens } from 'marked' import type { MarkedExtension, Tokens } from 'marked'
import { getStyleString } from '.'
/** /**
* https://github.com/bent10/marked-extensions/tree/main/packages/alert * 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],
...options.theme?.block[`${titleClassName}-${variantType}`], ...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`, name: `alert`,
level: `block`, level: `block`,
renderer({ meta, tokens = [] }) { renderer({ meta, tokens = [] }) {
let tmpl = `<div class="${meta.className} ${meta.className}-${meta.variant}" style='${Object.entries(meta.style ?? {}).map(([key, value]) => `${key}: ${value}`).join(`; `)}'>\n` let tmpl = `<div class="${meta.className} ${meta.className}-${meta.variant}" style='${getStyleString(meta.style)}'>\n`
tmpl += `<p class="${meta.titleClassName}" style='${Object.entries(meta.titleStyle ?? {}).map(([key, value]) => `${key}: ${value}`).join(`; `)}'>` tmpl += `<p class="${meta.titleClassName}" style='${getStyleString(meta.titleStyle)}'>`
tmpl += meta.icon.replace( tmpl += meta.icon.replace(
`<svg`, `<svg`,
`<svg style="fill: ${meta.titleStyle?.color ?? `inherit`}"`, `<svg style="fill: ${meta.titleStyle?.color ?? `inherit`}"`,
) )
tmpl += meta.title tmpl += meta.title
tmpl += `</p>\n` tmpl += `</p>\n`
tmpl += this.parser.parse(tokens) tmpl += `<div style="${getStyleString(meta.contentWrapperStyle)}">${this.parser.parse(tokens)}</div>`
tmpl += `</div>\n` tmpl += `</div>\n`
return tmpl return tmpl

View File

@ -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 type { PropertiesHyphen } from 'csstype'
import { prefix } from '@/config' import { prefix } from '@/config'
@ -116,6 +116,15 @@ export function css2json(css: string): Partial<Record<Block | Inline, Properties
return json return json
} }
/**
* CSS
* @param {ExtendedProperties} style -
* @returns {string} - CSS
*/
export function getStyleString(style: ExtendedProperties) {
return Object.entries(style ?? {}).map(([key, value]) => `${key}: ${value}`).join(`; `)
}
/** /**
* *
* @param {string} content - * @param {string} content -

View File

@ -6,6 +6,7 @@ import hljs from 'highlight.js'
import { marked } from 'marked' import { marked } from 'marked'
import mermaid from 'mermaid' import mermaid from 'mermaid'
import { getStyleString } from '.'
import markedAlert from './MDAlert' import markedAlert from './MDAlert'
import { MDKatex } from './MDKatex' import { MDKatex } from './MDKatex'
@ -59,9 +60,7 @@ function getStyles(styleMapping: ThemeStyles, tokenName: string, addition: strin
if (!dict) { if (!dict) {
return `` return ``
} }
const styles = Object.entries(dict) const styles = getStyleString(dict)
.map(([key, value]) => `${key}:${value}`)
.join(`;`)
return `style="${styles}${addition}"` return `style="${styles}${addition}"`
} }