forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileHelpers.ts
More file actions
116 lines (106 loc) · 3.38 KB
/
Copy pathfileHelpers.ts
File metadata and controls
116 lines (106 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/// <reference path="../types/prettier.d.ts" />
import parserBabel from 'prettier/parser-babel'
import parserMarkdown from 'prettier/parser-markdown'
import parserPostcss from 'prettier/parser-postcss'
import * as prettier from 'prettier/standalone'
/**
* 通用文件下载函数
* @param content - 文件内容
* @param filename - 文件名
* @param mimeType - MIME 类型,默认为 text/plain
*/
export function downloadFile(content: string, filename: string, mimeType: string = `text/plain`) {
if (typeof document === `undefined`) {
throw new TypeError(`downloadFile can only be used in browser environment`)
}
const downLink = document.createElement(`a`)
downLink.download = filename
downLink.style.display = `none`
let objectUrl: string | null = null
// 检查是否是 base64 data URL
if (content.startsWith(`data:`) || content.startsWith(`blob:`)) {
downLink.href = content
}
else if (mimeType === `text/html`) {
downLink.href = `data:text/html;charset=utf-8,${encodeURIComponent(content)}`
}
else {
const blob = new Blob([content], { type: mimeType })
objectUrl = URL.createObjectURL(blob)
downLink.href = objectUrl
}
document.body.appendChild(downLink)
downLink.click()
document.body.removeChild(downLink)
// 如果是 blob URL,释放内存
if (objectUrl) {
URL.revokeObjectURL(objectUrl)
}
}
/**
* 将文件转换为 Base64 格式
* @param file - 要转换的文件
* @returns Base64 字符串的 Promise
*/
export function toBase64(file: Blob): Promise<string> {
return new Promise<string>((resolve, reject) => {
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => resolve((reader.result as string).split(`,`).pop()!)
reader.onerror = error => reject(error)
})
}
/**
* 根据数据生成 Markdown 表格
* @param options - 表格选项
* @param options.data - 表格数据对象
* @param options.rows - 表格行数
* @param options.cols - 表格列数
* @returns 生成的 Markdown 表格字符串
*/
export function createTable({ data, rows, cols }: {
data: { [k: string]: string }
rows: number
cols: number
}): string {
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}`] || ` `)
}
table += currRow.join(` | `)
table += ` |\n`
}
return table
}
/**
* 格式化文档内容
* @param content - 要格式化的内容
* @param type - 内容类型,决定使用的解析器,默认为 'markdown'
* @returns 格式化后的内容
*/
export async function formatDoc(content: string, type: `markdown` | `css` | `javascript` = `markdown`): Promise<string> {
const parser = type === `css` ? `css` : type === `javascript` ? `babel` : `markdown`
const plugins = type === `css` ? [parserPostcss] : type === `javascript` ? [parserBabel] : [parserMarkdown, parserBabel]
return await prettier.format(content, {
parser,
plugins,
// prettier v2.8.8 配置选项
printWidth: 80,
tabWidth: 2,
useTabs: false,
semi: false,
singleQuote: true,
quoteProps: `as-needed`,
trailingComma: `es5`,
bracketSpacing: true,
bracketSameLine: false,
arrowParens: `avoid`,
proseWrap: `preserve`,
htmlWhitespaceSensitivity: `css`,
endOfLine: `lf`,
})
}