@zwa73/utils 1.0.268 → 1.0.270

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.
@@ -5,17 +5,23 @@ type ExtractBlockOpt = {
5
5
  /**将标题从结构中排除 */
6
6
  trimTitle: boolean;
7
7
  };
8
+ type FromArg = {
9
+ data?: Record<string, any>;
10
+ content?: string;
11
+ };
8
12
  /**Markdown解析器 */
9
13
  export declare class Markdown {
10
- data: Record<string, any>;
11
- content: string;
12
- constructor(data: Record<string, any>, content: string);
14
+ data?: Record<string, any> | undefined;
15
+ content?: string | undefined;
16
+ private constructor();
13
17
  /**从内容文本构建Md */
14
18
  static parse(ctx?: string): Promise<Markdown>;
15
19
  /**从文件构建Md */
16
20
  static fromFile(filepath: string): Promise<Markdown>;
21
+ /**从数据构建 */
22
+ static from(arg: FromArg): Markdown;
17
23
  /**输出md文本 */
18
- stringify(opt: Exclude<Parameters<typeof stringify>[2], string | number>): Promise<string>;
24
+ stringify(opt: Exclude<Parameters<typeof stringify>[2], string | number>): string;
19
25
  /**根据标题提取块
20
26
  * @param opt - 选项
21
27
  */
@@ -24,7 +24,7 @@ class Markdown {
24
24
  }
25
25
  /**从内容文本构建Md */
26
26
  static async parse(ctx = '') {
27
- const { data, content } = (await getMatter())(ctx);
27
+ const { data, content } = (await getMatter())(ctx.replace(/\r\n/g, '\n'));
28
28
  return new Markdown(data, content);
29
29
  }
30
30
  /**从文件构建Md */
@@ -32,9 +32,13 @@ class Markdown {
32
32
  const context = (await fs_1.default.promises.readFile(filepath, 'utf-8')).replace(/\r\n/g, '\n');
33
33
  return Markdown.parse(context);
34
34
  }
35
+ /**从数据构建 */
36
+ static from(arg) {
37
+ return new Markdown(arg.data, arg.content);
38
+ }
35
39
  /**输出md文本 */
36
- async stringify(opt) {
37
- const yamlText = Object.values(this.data).length > 0 ? `---\n${(0, yaml_1.stringify)(this.data, opt)}---\n` : '';
40
+ stringify(opt) {
41
+ const yamlText = Object.values(this.data ?? {}).length > 0 ? `---\n${(0, yaml_1.stringify)(this.data, opt)}---\n` : '';
38
42
  return `${yamlText}${this.content}`;
39
43
  //return (await getMatter()).stringify(this.content,this.data);
40
44
  }
@@ -56,7 +60,7 @@ class Markdown {
56
60
  * - (?=\n${level} |$):直到下一个同级标题或文件结束
57
61
  */
58
62
  const pattern = new RegExp(`(^|\\n)${level} ${content}[\\s\\S]*?(?=\\n${level} |$)`);
59
- const match = this.content.match(pattern);
63
+ const match = (this.content ?? '').match(pattern);
60
64
  const result = match ? match[0].trim() : undefined;
61
65
  return trimTitle && result !== undefined
62
66
  ? result.replace(new RegExp(`^${level} ${content}\\n?`), '')
@@ -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";
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.268",
3
+ "version": "1.0.270",
4
4
  "description": "my utils",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {