-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
89 lines (87 loc) · 2.68 KB
/
main.js
File metadata and controls
89 lines (87 loc) · 2.68 KB
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
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// main.ts
var main_exports = {};
__export(main_exports, {
default: () => AutoListStylesPlugin
});
module.exports = __toCommonJS(main_exports);
var import_obsidian = require("obsidian");
var AutoListStylesPlugin = class extends import_obsidian.Plugin {
async onload() {
this.registerMarkdownPostProcessor((el) => {
el.querySelectorAll("ol").forEach((list) => {
const firstLi = list.querySelector("li");
if (firstLi) {
const firstText = getFirstTextNode(firstLi);
if (firstText && firstText.textContent) {
const indentLevel = getIndentLevel(list);
const listStyleType = getListStyleType(indentLevel);
list.style.listStyleType = listStyleType;
firstText.textContent = firstText.textContent.trim();
}
}
});
});
}
};
function getFirstTextNode(node) {
if (node == null)
return null;
for (let i = 0; i < node.childNodes.length; i++) {
if (node.childNodes[i].nodeType === Node.TEXT_NODE) {
return node.childNodes[i];
} else if (node.childNodes[i].hasChildNodes()) {
const childText = getFirstTextNode(node.childNodes[i]);
if (childText) {
return childText;
}
}
}
return null;
}
function getIndentLevel(list) {
let indentLevel = 0;
let parent = list.parentElement;
while (parent) {
if (parent.tagName === "OL") {
indentLevel++;
}
parent = parent.parentElement;
}
return indentLevel;
}
function getListStyleType(indentLevel) {
const defaultStyles = [
"decimal",
"lower-alpha",
"lower-roman",
"upper-alpha",
"upper-roman",
"decimal-leading-zero",
"disc",
"circle",
"square"
];
const defaultStyleIndex = indentLevel % defaultStyles.length;
return defaultStyles[defaultStyleIndex];
}