@zwa73/utils 1.0.267 → 1.0.269
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { stringify } from 'yaml';
|
|
2
|
+
type ExtractBlockOpt = {
|
|
3
|
+
/**要提取的标题 必须为 "# 标题" 的格式 */
|
|
4
|
+
title: string;
|
|
5
|
+
/**将标题从结构中排除 */
|
|
6
|
+
trimTitle: boolean;
|
|
7
|
+
};
|
|
8
|
+
type FromArg = {
|
|
9
|
+
data?: Record<string, any>;
|
|
10
|
+
content?: string;
|
|
11
|
+
};
|
|
12
|
+
/**Markdown解析器 */
|
|
13
|
+
export declare class Markdown {
|
|
14
|
+
data?: Record<string, any> | undefined;
|
|
15
|
+
content?: string | undefined;
|
|
16
|
+
private constructor();
|
|
17
|
+
/**从内容文本构建Md */
|
|
18
|
+
static parse(ctx?: string): Promise<Markdown>;
|
|
19
|
+
/**从文件构建Md */
|
|
20
|
+
static fromFile(filepath: string): Promise<Markdown>;
|
|
21
|
+
/**从数据构建 */
|
|
22
|
+
static from(arg: FromArg): Promise<Markdown>;
|
|
23
|
+
/**输出md文本 */
|
|
24
|
+
stringify(opt: Exclude<Parameters<typeof stringify>[2], string | number>): Promise<string>;
|
|
25
|
+
/**根据标题提取块
|
|
26
|
+
* @param opt - 选项
|
|
27
|
+
*/
|
|
28
|
+
extractBlock(opt: ExtractBlockOpt): string | undefined;
|
|
29
|
+
/**根据标题提取块内第一个json代码块
|
|
30
|
+
* @param opt - 选项
|
|
31
|
+
*/
|
|
32
|
+
extractJson(title: string): string | undefined;
|
|
33
|
+
}
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Markdown = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const js_utils_1 = require("@zwa73/js-utils");
|
|
9
|
+
const UtilLogger_1 = require("../UtilLogger");
|
|
10
|
+
const yaml_1 = require("yaml");
|
|
11
|
+
const getMatter = async () => {
|
|
12
|
+
const matter = await js_utils_1.JsFunc.dynamicImport('gray-matter');
|
|
13
|
+
if (matter == undefined)
|
|
14
|
+
js_utils_1.JsFunc.throwError(`npm包 gray-matter 动态导入失败, 此包不会随@zwa73/utils自动安装, 可能是未安装, 可使用 npm i gray-matter@4.0.3 来安装`);
|
|
15
|
+
return matter;
|
|
16
|
+
};
|
|
17
|
+
/**Markdown解析器 */
|
|
18
|
+
class Markdown {
|
|
19
|
+
data;
|
|
20
|
+
content;
|
|
21
|
+
constructor(data, content) {
|
|
22
|
+
this.data = data;
|
|
23
|
+
this.content = content;
|
|
24
|
+
}
|
|
25
|
+
/**从内容文本构建Md */
|
|
26
|
+
static async parse(ctx = '') {
|
|
27
|
+
const { data, content } = (await getMatter())(ctx.replace(/\r\n/g, '\n'));
|
|
28
|
+
return new Markdown(data, content);
|
|
29
|
+
}
|
|
30
|
+
/**从文件构建Md */
|
|
31
|
+
static async fromFile(filepath) {
|
|
32
|
+
const context = (await fs_1.default.promises.readFile(filepath, 'utf-8')).replace(/\r\n/g, '\n');
|
|
33
|
+
return Markdown.parse(context);
|
|
34
|
+
}
|
|
35
|
+
/**从数据构建 */
|
|
36
|
+
static async from(arg) {
|
|
37
|
+
return new Markdown(arg.data, arg.content);
|
|
38
|
+
}
|
|
39
|
+
/**输出md文本 */
|
|
40
|
+
async stringify(opt) {
|
|
41
|
+
const yamlText = Object.values(this.data ?? {}).length > 0 ? `---\n${(0, yaml_1.stringify)(this.data, opt)}---\n` : '';
|
|
42
|
+
return `${yamlText}${this.content}`;
|
|
43
|
+
//return (await getMatter()).stringify(this.content,this.data);
|
|
44
|
+
}
|
|
45
|
+
/**根据标题提取块
|
|
46
|
+
* @param opt - 选项
|
|
47
|
+
*/
|
|
48
|
+
extractBlock(opt) {
|
|
49
|
+
const { title, trimTitle = false } = opt;
|
|
50
|
+
// 匹配标题格式,要求为 "# 标题" 或 "## 标题" 等
|
|
51
|
+
const matchTitle = title.match(/(#+) (.+)/);
|
|
52
|
+
if (matchTitle == null) {
|
|
53
|
+
UtilLogger_1.SLogger.warn(`${title} 不是有效的md标题, 必须为 "(#+) (标题)"`);
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
const [, level, content] = matchTitle;
|
|
57
|
+
/**构造正则:
|
|
58
|
+
* - (^|\n)${level} ${content}:匹配标题行(支持开头或换行)
|
|
59
|
+
* - [\s\S]*?:非贪婪匹配标题后的所有内容
|
|
60
|
+
* - (?=\n${level} |$):直到下一个同级标题或文件结束
|
|
61
|
+
*/
|
|
62
|
+
const pattern = new RegExp(`(^|\\n)${level} ${content}[\\s\\S]*?(?=\\n${level} |$)`);
|
|
63
|
+
const match = (this.content ?? '').match(pattern);
|
|
64
|
+
const result = match ? match[0].trim() : undefined;
|
|
65
|
+
return trimTitle && result !== undefined
|
|
66
|
+
? result.replace(new RegExp(`^${level} ${content}\\n?`), '')
|
|
67
|
+
: result;
|
|
68
|
+
}
|
|
69
|
+
/**根据标题提取块内第一个json代码块
|
|
70
|
+
* @param opt - 选项
|
|
71
|
+
*/
|
|
72
|
+
extractJson(title) {
|
|
73
|
+
return this.extractBlock({ title, trimTitle: true })?.trim().replace(/```json\n([\s\S]+)\n```/, '$1');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.Markdown = Markdown;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export * from './Hbs';
|
|
2
|
+
export * from './Markdown';
|
|
2
3
|
export type { EventData, DListMiddleNode, DListHeadNode, DListTailNode, DListInvalidNode, DListMaybeNode, DListNode, BridgeInterface } from "@zwa73/js-utils";
|
|
3
4
|
export { Stream, Spool, SmartCache, SequenceQueue, PromiseQueue, Piper, EventSystem, DLinkedList, DelayQueue, Bridge } from "@zwa73/js-utils";
|
package/dist/UtilClass/index.js
CHANGED
|
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
exports.Bridge = exports.DelayQueue = exports.DLinkedList = exports.EventSystem = exports.Piper = exports.PromiseQueue = exports.SequenceQueue = exports.SmartCache = exports.Spool = exports.Stream = void 0;
|
|
18
18
|
__exportStar(require("./Hbs"), exports);
|
|
19
|
+
__exportStar(require("./Markdown"), exports);
|
|
19
20
|
var js_utils_1 = require("@zwa73/js-utils");
|
|
20
21
|
Object.defineProperty(exports, "Stream", { enumerable: true, get: function () { return js_utils_1.Stream; } });
|
|
21
22
|
Object.defineProperty(exports, "Spool", { enumerable: true, get: function () { return js_utils_1.Spool; } });
|
package/dist/UtilFileTools.js
CHANGED
|
@@ -144,7 +144,7 @@ var UtilFT;
|
|
|
144
144
|
UtilFT.ensurePathExistsSync = ensurePathExistsSync;
|
|
145
145
|
function loadJSONFileSync(filePath, opt) {
|
|
146
146
|
try {
|
|
147
|
-
if (opt?.forceExt !== true && pathe_1.default.extname(filePath)
|
|
147
|
+
if (opt?.forceExt !== true && pathe_1.default.extname(filePath) == '')
|
|
148
148
|
filePath += '.json';
|
|
149
149
|
const str = pathExistsSync(filePath)
|
|
150
150
|
? fs.readFileSync(filePath, "utf-8")
|
|
@@ -167,7 +167,7 @@ var UtilFT;
|
|
|
167
167
|
UtilFT.loadJSONFileSync = loadJSONFileSync;
|
|
168
168
|
async function loadJSONFile(filePath, opt) {
|
|
169
169
|
try {
|
|
170
|
-
if (opt?.forceExt !== true && pathe_1.default.extname(filePath)
|
|
170
|
+
if (opt?.forceExt !== true && pathe_1.default.extname(filePath) == '')
|
|
171
171
|
filePath += '.json';
|
|
172
172
|
const str = (await pathExists(filePath))
|
|
173
173
|
? await fs.promises.readFile(filePath, "utf-8")
|
|
@@ -202,7 +202,7 @@ var UtilFT;
|
|
|
202
202
|
*/
|
|
203
203
|
async function writeJSONFile(filePath, token, opt) {
|
|
204
204
|
const str = UtilFunctions_1.UtilFunc.stringifyJToken(token, opt);
|
|
205
|
-
if (opt?.forceExt !== true && pathe_1.default.extname(filePath)
|
|
205
|
+
if (opt?.forceExt !== true && pathe_1.default.extname(filePath) == '')
|
|
206
206
|
filePath += '.json';
|
|
207
207
|
// 判断文件路径是否存在 不存在则创建
|
|
208
208
|
if (!(await pathExists(filePath)))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zwa73/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.269",
|
|
4
4
|
"description": "my utils",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
"pathe": "^1.1.2",
|
|
28
28
|
"querystring": "^0.2.1",
|
|
29
29
|
"winston": "^3.10.0",
|
|
30
|
-
"winston-daily-rotate-file": "^4.7.1"
|
|
30
|
+
"winston-daily-rotate-file": "^4.7.1",
|
|
31
|
+
"yaml": "^2.8.1"
|
|
31
32
|
},
|
|
32
33
|
"devDependencies": {
|
|
33
34
|
"@deepkit/type": "^1.0.1-alpha.153",
|