-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExportToMd.js
173 lines (137 loc) · 10.8 KB
/
ExportToMd.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var config = {
mdPath:"E:/01 Personal Doc/10 Blog/01 hexo-blog/source/_posts/",
imgPath:"E:/01 Personal Doc/10 Blog/01 hexo-blog/source/images/",
showRootDir: true
}
function OnExportMDButtonClicked() {
// var objBrowser = WizExplorerApp.Window.CurrentDocumentBrowserObject;
// objBrowser.ExecuteScript("document.body.innerText", function(docText) {
// objWindow.ShowMessage("文档内容为空, 导出失败" + docText, "提示", 0x40);
// });
var objApp = WizExplorerApp,
objWindow = objApp.Window,
objDocument = objWindow.CurrentDocument,
objCommon = objApp.CreateWizObject("WizKMControls.WizCommonUI"),
tempDocument = getTempDocumentInfo(objCommon, objDocument);
if (!tempDocument.text) {
objWindow.ShowMessage("文档内容为空, 导出失败", "提示", 0x40);
return false;
}
tempDocument.text = modifyDocument(objCommon, objDocument, tempDocument.text);
saveFile(objCommon, objDocument, objWindow, tempDocument);
}
function getTempDocumentInfo(objCommon, objDocument) {
var tempPath = objCommon.GetSpecialFolder("TemporaryFolder") + "export_md_temp/";
objCommon.CreateDirectory(tempPath);
tempPath += objDocument.GUID + "/";
objCommon.CreateDirectory(tempPath);
var tempImgPath = tempPath + "index_files/",
tempFile = tempPath + "index.html";
objCommon.CreateDirectory(tempImgPath);
objDocument.SaveToHtml(tempFile, 0);
return {
text: convertHtmlToText(objCommon.LoadTextFromFile(tempFile)),
imagePath: tempImgPath
}
}
function convertHtmlToText(text) {
var match = text.match('<body>(.*)</body>');
text = match[1];
text = text.replace(/<br\/>/gm, '\n');
text = text.replace(/</gm, '<');
text = text.replace(/>/gm, '>');
return text;
}
function modifyDocument(objCommon, objDocument, text) {
text = setHeadInfo(objCommon, objDocument, text);
text = setImagePath(text);
text = deleteEdtag(text);
return text;
}
function setImagePath(text) {
return text.replace(/index_files/g,"/images");
}
function deleteDescLabel(text) {
return text = text.replace(/---[\s\S]*?---/gm, '')
}
function deleteEdtag (text) {
//fix unuseful content bug
return text.replace(/<ed_tag.*?<\/ed_tag>/g, '');
}
function setHeadInfo(objCommon, objDocument, text) {
var categories = objDocument.Parent.Name,
dtCreated = new Date(objDocument.DateCreated),
timeCreated = objCommon.ToLocalDateString(dtCreated, false) + " " + objCommon.ToLocalTimeString(dtCreated),
location = objDocument.Location;
if (config.showRootDir && location.match(/^\/([\w-]+)\/?/)) {
categories = location.match(/^\/([\w-\s]+)\/?/)[1]
}
var head = addHeadToDocument(text, {
title: objDocument.Title.replace(/\.md$/g, ''),
tags: objDocument.Tags,
date: timeCreated,
categories: categories
})
text = deleteDescLabel(text)
text = head + text;
return text;
}
function saveFile(objCommon, objDocument, objWindow, tempDocument) {
//save file
var filename = config['mdPath'] + objDocument.Title.replace(/\.md$/g, '') + ".md";
objCommon.SaveTextToFile(filename, tempDocument.text, "utf-8");
//save image. copy file from tempory to target folder 复制图片
var path = objCommon.EnumFiles2(tempDocument.imagePath, "*.*", false);
if(!path) {
objWindow.ShowMessage("文档无图片,导出完成", "提示", 0x40);
return true;
}
var imgPath = config['imgPath'],
pathArr = path.split("\n"),
len = pathArr.length,
curPath = "",
name = "";
for (var i = 0; i< len; i++) {
curPath = pathArr[i];
name = curPath.substring(curPath.lastIndexOf("/") + 1, curPath.length);;
curPath = imgPath + name;
objCommon.CopyFile(pathArr[i], curPath);
}
objWindow.ShowMessage("文档有图片,导出完成", "提示", 0x40);
}
function addHeadToDocument(text, docInfo){
var tags, moreLabels;
//tags
var ret = [];
for (var i = 0; i < docInfo.tags.Count; i++) {
ret.push(docInfo.tags.Item(i).Name);
}
tags = ret.join(",");
var exec = /---([\s\S]*?)---/gm.exec(text)
moreLabels = exec ? exec[1].replace(/^\s*|\s*$/g, '').replace(/!\[\]\((.*?)\)/g, '$1') : '';
//morelabels
if (moreLabels) {
var encodeLabels = encodeURIComponent(moreLabels);
encodeLabels = encodeLabels.replace(/%3A%C2%A0/g, '%3A%20')
moreLabels = decodeURIComponent(encodeLabels);
}
moreLabels = moreLabels || '\n';
var head = "---" + "\n"
+ "title: " + docInfo.title + "\n"
+ "date: " + docInfo.date + "\n"
+ "categories: " + docInfo.categories + "\n"
+ "tags: [" + tags + "]\n"
+ "comments: " + "true" + "\n"
+ "toc: " + "true" + "\n"
+ moreLabels + "\n"
+ "---" + "\n";
return head;
}
function InitExoprtToMdButton() {
var pluginPath = objApp.GetPluginPathByScriptFileName("ExportToMd.js");
var languangeFileName = pluginPath + "plugin.ini";
//strExport is key in plugin.ini file
var buttonText = objApp.LoadStringFromFile(languangeFileName, "strExport");
objWindow.AddToolButton("document", "ExportButton", buttonText, "", "OnExportMDButtonClicked");
}
InitExoprtToMdButton();