-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblogStateChange.js
62 lines (52 loc) · 2.25 KB
/
blogStateChange.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
if (api.originEntity.isDeleted) {
return;
}
if (api.originEntity.name != "publish") { return }
const note = api.originEntity.getNote();
const timeLineRoot = api.getNoteWithLabel("timeLineRoot");
const unpublishedRoot = api.getNoteWithLabel("unpublishedRoot");
const publishStatus = note.getLabelValue("publish");
const category = note.getLabelValue("category");
const categoryNote = api.getNoteWithLabel("categoryName", category);
const tags = note.getLabelValues("tags");
const lastTagString = note.getLabelValue("lastTags");
const tagsRoot = api.getNoteWithLabel("blogTagRoot");
if (category != "说说") {
api.toggleNoteInParent(publishStatus === "true", note.noteId, timeLineRoot.noteId);
}
api.toggleNoteInParent(publishStatus === "true", note.noteId, categoryNote.noteId);
if (publishStatus === "false") {
api.toggleNoteInParent(true, note.noteId, unpublishedRoot.noteId);
if (lastTagString != null && lastTagString != undefined && lastTagString != "") {
const lastTags = lastTagString.split("|");
lastTags.forEach((lastTag) => {
api.toggleNoteInParent(false, note.noteId, api.getNoteWithLabel("blogTag", lastTag).noteId);
note.setLabel("lastTagString", "");
})
}
} else {
const content = note.getContent();
note.setLabel("summary", extractTextFromHTML(content));
tags.forEach((tag) => {
const tagNote = api.getNoteWithLabel("blogTag", tag);
if (tagNote === null || tagNote === undefined || tagNote === "") {
const resp = api.createTextNote(tagsRoot.noteId, tag, "");
resp.note.setLabel("blogTag", tag);
resp.note.setLabel("shareAlias", `tag_${tag}`);
api.toggleNoteInParent(true, note.noteId, resp.note.noteId);
} else {
api.toggleNoteInParent(true, note.noteId, tagNote.noteId);
}
});
note.setLabel("lastTags", tags.join("|"));
if (unpublishedRoot.getChildNotes().some(ele => ele.noteId === note.noteId)) {
api.toggleNoteInParent(false, note.noteId, unpublishedRoot.noteId);
}
}
note.save();
function extractTextFromHTML(html) {
var text = html.replace(/<[^>]*>/g, '');
text = text.replace(/\s+/g, '');
var extractedText = text.substring(0, 120);
return `${extractedText}...`;
}