generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helperFunctions.ts
127 lines (89 loc) · 3.56 KB
/
helperFunctions.ts
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
import ReleaseTimeline from "main";
import { App } from 'obsidian';
export {createErrorMsg, createRowSeparator, createRowSeparatorYearMonth, createRowSeparatorWeek, createRowYear, createRowItem, createNewRow, parseQuerySortOrder, replaceThisInQuery };
function createErrorMsg(errorText) {
const errorTbl = createEl("table", { cls: "release-timeline" } );
const newI = createEl("i", {text: errorText})
errorTbl.appendChild(newI);
return errorTbl;
}
function createNewRow(...args) {
const newRow = document.createElement("tr");
args.forEach((arg, index) => {
newRow.appendChild(arg);
});
return newRow;
}
function createRowSeparator( { cls } = {} ) {
const newTdSeparator = createEl("td", { cls: "td-separator" });
if( typeof cls !== 'undefined' ) {newTdSeparator.setAttribute("class", cls)};
const rowSeparator = createEl("tr");
rowSeparator.appendChild(newTdSeparator);
return rowSeparator;
}
function createRowSeparatorYearMonth(type: string) {
const newTdSeparator1 = createEl("td", { cls: "td-separator" });
const newTdSeparator2 = createEl("td", { cls: "td-separator" });
const newTdSeparator3 = createEl("td", { cls: "td-separator" });
if (type == 'border') {
newTdSeparator1.setAttribute("class", "line-separator");
newTdSeparator2.setAttribute("class", "line-separator");
newTdSeparator3.setAttribute("class", "line-separator");
}
const newRow = createNewRow(newTdSeparator1, newTdSeparator2, newTdSeparator3);
return newRow;
}
function createRowSeparatorWeek(type: string) {
const newTdSeparator1 = createEl("td", { cls: "td-separator" });
newTdSeparator1.setAttribute("colspan", "4");
if (type == 'border') {
newTdSeparator1.setAttribute("class", "line-separator");
}
const newRow = createNewRow(newTdSeparator1);
return newRow;
}
function createRowYear( { val, cls, rowspanNb } = {} ) {
const newTh = createEl("th", {text: val})
newTh.setAttribute("scope", "row");
newTh.setAttribute("class", cls);
if ( typeof rowspanNb !== 'undefined' ) { newTh.setAttribute("rowspan", rowspanNb) };
return newTh;
}
function createRowItem( { fileName, fileAlias, cls } = {} ) {
const newTd = document.createElement("td");
if ( typeof cls !== 'undefined' ) { newTd.setAttribute("class", cls) };
newTd.classList.add("bullet-points");
const newLink = createEl("a", {cls: "internal-link", text: fileAlias});
newLink.setAttribute("data-href", fileName);
newTd.appendChild(newLink);
return newTd;
}
function parseQuerySortOrder(content: string, plugin:ReleaseTimeline) {
let regExSortOrder = /sort(?:.*)? (desc|asc)/;
let settingsSort = plugin.settings.defaultSortOrder;
content = content.replace(/[\r\n]+/g," ").toLocaleLowerCase();
let querySortOrderMatch = content.match(regExSortOrder);
let querySortOrder;
if (querySortOrderMatch === null) {
return settingsSort;
} else {
querySortOrder = querySortOrderMatch[1].trim();
return querySortOrder;
}
}
function replaceThisInQuery(query: string, app: App) {
const regex = /([\n \(])this\./g;
let activeFileName = "";
try {
activeFileName = app.workspace.getActiveFile().basename;
}
catch(error) {
return query;
}
const newQuery = query.replace(regex, (match, precedingChar) => {
return `${precedingChar}[[${activeFileName}]].`;
});
console.log(activeFileName);
console.log(newQuery);
return newQuery;
}