md/src/utils/index.ts

320 lines
8.9 KiB
TypeScript
Raw Normal View History

import type { Block, Inline, Theme } from '@/types'
2020-02-15 21:44:42 +08:00
import type { PropertiesHyphen } from 'csstype'
import { prefix } from '@/config'
import juice from 'juice'
2024-08-31 11:27:26 +08:00
import * as prettierPluginBabel from 'prettier/plugins/babel'
import * as prettierPluginEstree from 'prettier/plugins/estree'
import * as prettierPluginMarkdown from 'prettier/plugins/markdown'
2024-08-30 15:17:45 +08:00
import * as prettierPluginCss from 'prettier/plugins/postcss'
import { format } from 'prettier/standalone'
2024-08-18 19:49:16 +08:00
2024-09-16 17:07:47 +08:00
export function addPrefix(str: string) {
2024-08-18 19:49:16 +08:00
return `${prefix}__${str}`
}
2024-09-16 17:07:47 +08:00
export function customizeTheme(theme: Theme, options: {
fontSize?: string
color?: string
}) {
2024-08-29 19:43:06 +08:00
const newTheme = JSON.parse(JSON.stringify(theme))
const { fontSize, color } = options
if (fontSize) {
for (let i = 1; i <= 4; i++) {
2024-08-29 19:43:06 +08:00
const v = newTheme.block[`h${i}`][`font-size`]
2024-09-16 17:07:47 +08:00
newTheme.block[`h${i}`][`font-size`] = `${Number(fontSize) * Number.parseFloat(v)}px`
}
}
2024-08-29 19:43:06 +08:00
if (color) {
newTheme.base[`--md-primary-color`] = color
}
2024-09-16 17:07:47 +08:00
return newTheme as Theme
}
2020-01-13 22:16:04 +08:00
2024-09-16 17:07:47 +08:00
export function customCssWithTemplate(jsonString: Partial<Record<Block | Inline, PropertiesHyphen>>, color: string, theme: Theme) {
2024-08-30 15:17:45 +08:00
const newTheme = customizeTheme(theme, { color })
2024-09-16 17:07:47 +08:00
const mergeProperties = <T extends Block | Inline = Block>(target: Record<T, PropertiesHyphen>, source: Partial<Record<Block | Inline, PropertiesHyphen>>, keys: T[]) => {
2024-08-30 15:17:45 +08:00
keys.forEach((key) => {
2024-08-29 19:43:06 +08:00
if (source[key]) {
2024-08-30 15:17:45 +08:00
target[key] = Object.assign(target[key] || {}, source[key])
2024-08-29 19:43:06 +08:00
}
2024-08-30 15:17:45 +08:00
})
}
2024-09-16 17:07:47 +08:00
const blockKeys: Block[] = [
2024-08-30 15:17:45 +08:00
`h1`,
`h2`,
`h3`,
`h4`,
`code`,
`code_pre`,
2024-08-30 15:17:45 +08:00
`p`,
`hr`,
`blockquote`,
`blockquote_p`,
`image`,
`ul`,
`ol`,
]
2024-09-16 17:07:47 +08:00
const inlineKeys: Inline[] = [`strong`, `codespan`, `link`, `wx_link`, `listitem`]
2024-08-30 15:17:45 +08:00
mergeProperties(newTheme.block, jsonString, blockKeys)
mergeProperties(newTheme.inline, jsonString, inlineKeys)
return newTheme
2020-01-13 22:16:04 +08:00
}
/**
2024-08-20 19:01:44 +08:00
* CSS JSON
2020-01-13 22:16:04 +08:00
*
2024-08-20 19:01:44 +08:00
* @param {string} css - CSS
* @returns {object} - JSON CSS
2020-01-13 22:16:04 +08:00
*/
2024-09-16 17:07:47 +08:00
export function css2json(css: string): Partial<Record<Block | Inline, PropertiesHyphen>> {
2024-08-20 19:01:44 +08:00
// 去除所有 CSS 注释
css = css.replace(/\/\*[\s\S]*?\*\//g, ``)
2024-09-16 17:07:47 +08:00
const json: Partial<Record<Block | Inline, PropertiesHyphen>> = {}
2024-08-20 19:01:44 +08:00
// 辅助函数:将声明数组转换为对象
2024-09-16 17:07:47 +08:00
const toObject = (array: any[]) =>
array.reduce<{ [k: string]: string }>((obj, item) => {
const [property, value] = item.split(`:`).map((part: string) => part.trim())
2024-08-20 19:01:44 +08:00
if (property)
obj[property] = value
return obj
}, {})
while (css.includes(`{`) && css.includes(`}`)) {
const lbracket = css.indexOf(`{`)
const rbracket = css.indexOf(`}`)
2024-08-20 19:01:44 +08:00
// 获取声明块并转换为对象
const declarations = css.substring(lbracket + 1, rbracket)
.split(`;`)
2024-08-18 19:49:16 +08:00
.map(e => e.trim())
2024-08-20 19:01:44 +08:00
.filter(Boolean)
2024-08-20 19:01:44 +08:00
// 获取选择器并去除空格
const selectors = css.substring(0, lbracket)
.split(`,`)
2024-09-16 17:07:47 +08:00
.map(selector => selector.trim()) as (Block | Inline)[]
2024-08-20 19:01:44 +08:00
const declarationObj = toObject(declarations)
// 将声明对象关联到相应的选择器
selectors.forEach((selector) => {
2024-08-20 19:01:44 +08:00
json[selector] = { ...(json[selector] || {}), ...declarationObj }
})
2020-01-13 22:16:04 +08:00
2024-08-20 19:01:44 +08:00
// 处理下一个声明块
css = css.slice(rbracket + 1).trim()
}
return json
2020-01-13 22:16:04 +08:00
}
2020-05-01 21:30:25 +08:00
/**
2024-08-30 15:17:45 +08:00
*
* @param {string} content -
* @param {'markdown' | 'css'} [type] - 使'markdown'
* @returns {Promise<string>} -
2020-05-01 21:30:25 +08:00
*/
2024-09-16 17:07:47 +08:00
export async function formatDoc(content: string, type: `markdown` | `css` = `markdown`) {
2024-08-30 15:17:45 +08:00
const plugins = {
markdown: [prettierPluginMarkdown, prettierPluginBabel, prettierPluginEstree],
css: [prettierPluginCss],
2024-08-18 19:49:16 +08:00
}
2020-05-01 21:30:25 +08:00
2024-08-30 15:17:45 +08:00
const parser = type in plugins ? type : `markdown`
return await format(content, {
parser,
plugins: plugins[parser],
})
2020-12-03 00:51:39 +08:00
}
2020-11-21 09:33:45 +08:00
/**
* Markdown
* @param {string} doc -
2020-11-21 09:33:45 +08:00
*/
2024-09-16 17:07:47 +08:00
export function downloadMD(doc: string) {
const downLink = document.createElement(`a`)
2020-07-13 00:26:29 +08:00
downLink.download = `content.md`
downLink.style.display = `none`
const blob = new Blob([doc])
2020-07-13 00:26:29 +08:00
downLink.href = URL.createObjectURL(blob)
document.body.appendChild(downLink)
downLink.click()
document.body.removeChild(downLink)
2020-08-31 20:48:22 +08:00
}
2020-10-13 00:07:14 +08:00
2021-10-14 20:12:19 +08:00
/**
* HTML
*/
export function exportHTML() {
2024-09-16 17:07:47 +08:00
const element = document.querySelector(`#output`)!
setStyles(element)
const htmlStr = element.innerHTML
const downLink = document.createElement(`a`)
2021-10-14 20:12:19 +08:00
downLink.download = `content.html`
downLink.style.display = `none`
2024-08-18 19:49:16 +08:00
const blob = new Blob([
`<html><head><meta charset="utf-8" /></head><body><div style="width: 750px; margin: auto;">${htmlStr}</div></body></html>`,
])
2021-10-14 20:12:19 +08:00
downLink.href = URL.createObjectURL(blob)
document.body.appendChild(downLink)
downLink.click()
document.body.removeChild(downLink)
2024-09-16 17:07:47 +08:00
function setStyles(element: Element) {
/**
* DOM
* @param {DOM } element DOM
* @param {} excludes 使
* @returns
*/
2024-09-16 17:07:47 +08:00
function getElementStyles(element: Element, excludes = [`width`, `height`]) {
const styles = getComputedStyle(element, null)
return Object.entries(styles)
.filter(
2024-08-18 19:49:16 +08:00
([key]) => styles.getPropertyValue(key) && !excludes.includes(key),
)
.map(([key, value]) => `${key}:${value};`)
.join(``)
}
switch (true) {
case isPre(element):
case isCode(element):
case isSpan(element):
element.setAttribute(`style`, getElementStyles(element))
}
if (element.children.length) {
2024-08-18 19:49:16 +08:00
Array.from(element.children).forEach(child => setStyles(child))
}
// 判断是否是包裹代码块的 pre 元素
2024-09-16 17:07:47 +08:00
function isPre(element: Element) {
return (
2024-08-18 19:49:16 +08:00
element.tagName === `PRE`
&& Array.from(element.classList).includes(`code__pre`)
)
}
// 判断是否是包裹代码块的 code 元素
2024-09-16 17:07:47 +08:00
function isCode(element: Element | null) {
if (element == null) {
return false
}
return element.tagName === `CODE`
}
// 判断是否是包裹代码字符的 span 元素
2024-09-16 17:07:47 +08:00
function isSpan(element: Element) {
return (
2024-08-18 19:49:16 +08:00
element.tagName === `SPAN`
&& (isCode(element.parentElement)
2024-09-17 16:13:24 +08:00
|| isCode((element.parentElement!).parentElement))
)
}
}
2021-10-14 20:12:19 +08:00
}
2020-10-13 00:07:14 +08:00
/**
2024-08-20 12:39:32 +08:00
* Markdown
*
* @param {object} options -
* @param {object} options.data -
* @param {number} options.rows -
* @param {number} options.cols -
* @returns {string} Markdown
2020-10-13 00:07:14 +08:00
*/
2024-09-16 17:07:47 +08:00
export function createTable({ data, rows, cols }: { data: { [k: string]: string }, rows: number, cols: number }) {
let table = ``
for (let i = 0; i < rows + 2; ++i) {
table += `| `
const currRow = []
for (let j = 0; j < cols; ++j) {
const rowIdx = i > 1 ? i - 1 : i
currRow.push(i === 1 ? `---` : data[`k_${rowIdx}_${j}`] || ` `)
2020-10-13 00:07:14 +08:00
}
table += currRow.join(` | `)
table += ` |\n`
}
2020-10-13 00:07:14 +08:00
return table
2020-10-20 11:43:11 +00:00
}
2020-12-04 00:57:46 +08:00
2024-09-16 17:07:47 +08:00
export function toBase64(file: Blob) {
2024-09-17 08:26:52 +08:00
return new Promise<string>((resolve, reject) => {
const reader = new FileReader()
reader.readAsDataURL(file)
2024-09-17 08:26:52 +08:00
reader.onload = () => resolve((reader.result as string).split(`,`).pop()!)
2024-08-18 19:49:16 +08:00
reader.onerror = error => reject(error)
})
}
2020-12-05 21:16:09 +08:00
2024-09-16 17:07:47 +08:00
export function checkImage(file: File) {
2024-08-20 12:39:32 +08:00
// 检查文件名后缀
2024-08-20 19:01:44 +08:00
const isValidSuffix = /\.(?:gif|jpe?g|png)$/i.test(file.name)
if (!isValidSuffix) {
return {
ok: false,
msg: `请上传 JPG/PNG/GIF 格式的图片`,
}
}
2024-08-20 12:39:32 +08:00
// 检查文件大小
const maxSizeMB = 10
if (file.size > maxSizeMB * 1024 * 1024) {
return {
ok: false,
2024-08-20 12:39:32 +08:00
msg: `由于公众号限制,图片大小不能超过 ${maxSizeMB}M`,
}
}
2024-08-20 12:39:32 +08:00
return { ok: true }
2020-12-05 21:16:09 +08:00
}
/**
*
2024-09-16 17:07:47 +08:00
* @param {string} str
2024-08-18 19:49:16 +08:00
* @returns string
*/
2024-09-16 17:07:47 +08:00
export function removeLeft(str: string) {
const lines = str.split(`\n`)
// 获取应该删除的空白符数量
const minSpaceNum = lines
2024-08-18 19:49:16 +08:00
.filter(item => item.trim())
2024-09-16 17:07:47 +08:00
.map(item => (item.match(/(^\s+)?/)!)[0].length)
.sort((a, b) => a - b)[0]
// 删除空白符
2024-08-18 19:49:16 +08:00
return lines.map(item => item.slice(minSpaceNum)).join(`\n`)
}
export function solveWeChatImage() {
2024-09-16 17:07:47 +08:00
const clipboardDiv = document.getElementById(`output`)!
2024-08-18 19:49:16 +08:00
const images = clipboardDiv.getElementsByTagName(`img`)
for (let i = 0; i < images.length; i++) {
const image = images[i]
2024-09-16 17:07:47 +08:00
const width = image.getAttribute(`width`)!
const height = image.getAttribute(`height`)!
2024-08-18 19:49:16 +08:00
image.removeAttribute(`width`)
image.removeAttribute(`height`)
image.style.width = width
image.style.height = height
}
}
2024-09-16 17:07:47 +08:00
export function mergeCss(html: string) {
2024-08-18 19:49:16 +08:00
return juice(html, {
inlinePseudoElements: true,
preserveImportant: true,
})
2021-11-26 23:42:56 +08:00
}