@zwa73/utils 1.0.297 → 1.0.299
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.
|
@@ -46,7 +46,7 @@ export declare class ObsidianDepot {
|
|
|
46
46
|
/**搜索info.md文件
|
|
47
47
|
* @param opt - 搜索选项
|
|
48
48
|
*/
|
|
49
|
-
static scanMarkdown(opt: ScanOpt): Promise<ScanResult[]>;
|
|
49
|
+
static scanMarkdown(opt: ScanOpt): Promise<(ScanResult)[]>;
|
|
50
50
|
/**搜索并更新info.md文件 */
|
|
51
51
|
static updateMarkdown(opt: UpdateOpt): Promise<void>;
|
|
52
52
|
}
|
|
@@ -10,6 +10,7 @@ const pathe_1 = __importDefault(require("pathe"));
|
|
|
10
10
|
const Constant_1 = require("../Constant");
|
|
11
11
|
const UtilFileTools_1 = require("../UtilFileTools");
|
|
12
12
|
const UtilFunctions_1 = require("../UtilFunctions");
|
|
13
|
+
const UtilLogger_1 = require("../UtilLogger");
|
|
13
14
|
const Markdown_1 = require("./Markdown");
|
|
14
15
|
class ObsidianDepot {
|
|
15
16
|
//static create(opt:ObsidianDepotOpt){return new ObsidianDepot(opt);}
|
|
@@ -34,31 +35,37 @@ class ObsidianDepot {
|
|
|
34
35
|
static async scanMarkdown(opt) {
|
|
35
36
|
const { pattern = "**/info.md", rootPath, jsonTitle, tomlTitle, } = opt;
|
|
36
37
|
const infoList = await UtilFileTools_1.UtilFT.fileSearchGlob(rootPath, pattern);
|
|
37
|
-
return await Promise.all(infoList.map(async (
|
|
38
|
-
const { dir } = pathe_1.default.parse(
|
|
39
|
-
const content = await fs_1.default.promises.readFile(
|
|
38
|
+
return await Promise.all(infoList.map(async (fp) => {
|
|
39
|
+
const { dir } = pathe_1.default.parse(fp);
|
|
40
|
+
const content = await fs_1.default.promises.readFile(fp, 'utf-8');
|
|
40
41
|
const markdown = await Markdown_1.Markdown.parse(content);
|
|
41
42
|
const codetype = (jsonTitle != undefined && markdown.extractBlock({ title: jsonTitle, trimTitle: true })) ? 'json'
|
|
42
43
|
: (tomlTitle != undefined && markdown.extractBlock({ title: tomlTitle, trimTitle: true })) ? 'toml'
|
|
43
44
|
: 'none';
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
45
|
+
try {
|
|
46
|
+
const codeobj = await UtilFunctions_1.UtilFunc.match(codetype, {
|
|
47
|
+
json: () => JSON.parse(markdown.extractCodeBlock({ title: jsonTitle, codetype: 'json' }) ?? ''),
|
|
48
|
+
toml: async () => (await (0, Constant_1.importOptionPack)({
|
|
49
|
+
name: '@iarna/toml',
|
|
50
|
+
installTip: 'npm i @iarna/toml@2.2.5'
|
|
51
|
+
})).parse(markdown.extractCodeBlock({ title: tomlTitle, codetype: 'toml' }) ?? ''),
|
|
52
|
+
none: () => undefined
|
|
53
|
+
});
|
|
54
|
+
return {
|
|
55
|
+
content,
|
|
56
|
+
abspath: dir,
|
|
57
|
+
relpath: pathe_1.default.relative(rootPath, dir),
|
|
58
|
+
infopath: fp,
|
|
59
|
+
markdown: await Markdown_1.Markdown.parse(content),
|
|
60
|
+
codetype,
|
|
61
|
+
codeobj,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
UtilLogger_1.SLogger.warn(`ObsidianDepot.scanMarkdown 错误 目标路径:${fp}`, err);
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
})).then(li => li.filter(v => v != undefined));
|
|
62
69
|
}
|
|
63
70
|
/**搜索并更新info.md文件 */
|
|
64
71
|
static async updateMarkdown(opt) {
|
|
@@ -66,11 +73,16 @@ class ObsidianDepot {
|
|
|
66
73
|
const scanResultList = await ObsidianDepot.scanMarkdown(opt);
|
|
67
74
|
await js_utils_1.Stream.from(scanResultList, 16).map(async (scanResult) => {
|
|
68
75
|
const { infopath } = scanResult;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
76
|
+
try {
|
|
77
|
+
const { content, data } = await updateFunc(scanResult);
|
|
78
|
+
await fs_1.default.promises.writeFile(infopath, await Markdown_1.Markdown.from({
|
|
79
|
+
data,
|
|
80
|
+
content
|
|
81
|
+
}).stringify(stringifyOpt));
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
UtilLogger_1.SLogger.warn(`ObsidianDepot.updateMarkdown 错误 目标路径:${infopath}`, err);
|
|
85
|
+
}
|
|
74
86
|
}).apply();
|
|
75
87
|
}
|
|
76
88
|
}
|