Skip to content

Commit 968267b

Browse files
committed
0.0.5
- Processor now parses attributes on `<ul>` elements correctly
1 parent 4d8a88e commit 968267b

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,30 @@ Both Wikilinks and markdown syntax links may have attributes placed on them.
6161

6262
### Lists
6363

64-
Lists may have attributes placed on each individual list item. At this time there is not a way to place attributes on a containing `<ul>`.
64+
Lists may have attributes placed on each individual list item.
6565

6666
```markdown
6767
- item {: .item}
6868
- nested item {: .nested}
6969
- nested item 2 {: id="item 2" }
7070
```
7171

72+
Attributes can only be applied to the final nested list by placing the attribute on the line immediately following the last item.
73+
74+
```markdown
75+
- item 1 {: .item}
76+
- item 2 {: id=item }
77+
- item 3 {: data-item=3 }
78+
{: .top-level-ul }
79+
```
80+
81+
```markdown
82+
- item {: .item}
83+
- nested item {: .nested}
84+
- nested item 2 {: id="item 2" }
85+
{: .nested-ul}
86+
```
87+
7288
### Code Blocks
7389

7490
Code blocks should have their attributes placed after the initial three ticks.

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "markdown-attributes",
33
"name": "Markdown Attributes",
4-
"version": "0.0.4",
4+
"version": "0.0.5",
55
"minAppVersion": "0.12.10",
66
"description": "Add markdown attributes to elements in Obsidian.md",
77
"author": "Jeremy Valentine",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "markdown-attributes",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Add markdown attributes to elements in Obsidian.md",
55
"main": "main.js",
66
"scripts": {

src/processor.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ export default class Processor {
88
static BASE_RE = /\{\:[ ]*([^\}\n ][^\}\n]*)[ ]*\}/;
99
static BLOCK_RE = /\n[ ]*\{\:?[ ]*([^\}\n ][^\}\n]*)[ ]*\}[ ]*$/;
1010

11+
constructor(private topLevelElement: HTMLElement) {}
12+
1113
static parse(el: HTMLElement) {
12-
return new Processor().recurseAndParseElements(el);
14+
return new Processor(el).recurseAndParseElements(el);
1315
}
1416

1517
/**
@@ -61,8 +63,6 @@ export default class Processor {
6163
for (let pair of trys) {
6264
if (!pair || !pair.length) continue;
6365

64-
65-
6666
//#id
6767
/* if (pair.charAt(0) === idChar) {
6868
attrs.push(["id", pair.slice(1)]);
@@ -103,20 +103,33 @@ export default class Processor {
103103

104104
// Text content of this node and *not* the children.
105105
const text = this.getTopLevelText(el);
106+
console.log("🚀 ~ file: processor.ts ~ line 102 ~ el", text);
106107

107108
if (Processor.BLOCK_RE.test(text)) {
108109
// Attributes should apply to the whole block.
110+
111+
let element = el;
112+
if (el instanceof HTMLLIElement) {
113+
// Need to apply attributes to containing UL if HTMLLIElement has a block attribute
114+
element = el.parentElement;
115+
}
116+
109117
let [original, attribute_string] =
110-
text.match(Processor.BASE_RE) ?? [];
118+
text.match(Processor.BLOCK_RE) ?? [];
111119
elements.push({
112-
element: el,
120+
element: element,
113121
attributes: this.getAttrs(attribute_string),
114122
text: attribute_string
115123
});
116124
el.innerHTML = el.innerHTML.replace(original, "");
125+
126+
//rerun parser if LI element to get inlines
127+
if (el instanceof HTMLLIElement) {
128+
elements.push(...this.recurseAndParseElements(el));
129+
}
117130
} else if (Processor.BASE_RE.test(text)) {
118131
// Attributes are inline.
119-
132+
console.log(Processor.BASE_RE.test(text));
120133
// Get the text nodes that contains the attribute string.
121134
let textNode = Array.from(el.childNodes).find(
122135
(node) =>
@@ -134,6 +147,10 @@ export default class Processor {
134147

135148
// Collapsible elements are a special case due to the collapse handle.
136149
if (sibling && sibling.hasClass("collapse-indicator")) {
150+
console.log(
151+
"🚀 ~ file: processor.ts ~ line 150 ~ Processor.BASE_RE.test(text)",
152+
Processor.BASE_RE.test(text)
153+
);
137154
sibling = sibling.parentElement;
138155
}
139156

versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"0.0.4": "0.12.0"
2+
"0.0.5": "0.12.0"
33
}

0 commit comments

Comments
 (0)