Skip to content

Commit a96d290

Browse files
committed
0.0.6
- Can now apply block level attributes to tables
1 parent 968267b commit a96d290

File tree

6 files changed

+57
-10
lines changed

6 files changed

+57
-10
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,25 @@ Attributes must be added to headers at the end of the line.
4343

4444
### Tables
4545

46-
Currently, there is not a way to add attributes to an entire table or table row. However, attributes can be added to individual table cells like so:
46+
Attributes can be added to the `<table>` element by placing the attribute on the line below it.
47+
48+
```markdown
49+
| header1 | header2 |
50+
| ------- | ------- |
51+
| column1 | column2 |
52+
{: .table-class}
53+
```
54+
55+
Attributes can be added to individual table cells like so:
4756

4857
```markdown
4958
| header1 {: .class} | header2 |
5059
| ------------------ | ---------------------- |
5160
| column1 | column2 {: .class-two} |
5261
```
5362

63+
It is not currently possible to add attributes to `<tr>` or `<thead>` elements.
64+
5465
### Links
5566

5667
Both Wikilinks and markdown syntax links may have attributes placed on them.

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.5",
4+
"version": "0.0.6",
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.5",
3+
"version": "0.0.6",
44
"description": "Add markdown attributes to elements in Obsidian.md",
55
"main": "main.js",
66
"scripts": {

src/main.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,47 @@ export default class MarkdownAttributes extends Plugin {
4343
child.prepend(new Text(attribute_string));
4444
}
4545

46+
/**
47+
* Table elements should check the next line in the source to see if it is a single block attribute,
48+
* because those block attributes are not applied to the table.
49+
*/
50+
if (child instanceof HTMLTableElement) {
51+
if (!ctx.getSectionInfo(topElement)) return;
52+
53+
/** Pull the Section data. */
54+
const { text, lineEnd } = ctx.getSectionInfo(topElement);
55+
56+
/** Get the source for this element. */
57+
let source = (
58+
text.split("\n").slice(lineEnd + 1, lineEnd + 2) ?? []
59+
).shift();
60+
61+
/** Test if the element contains attributes. */
62+
if (
63+
!source ||
64+
!source.length ||
65+
!Processor.ONLY_RE.test(source.trim())
66+
)
67+
return;
68+
69+
/** Pull the matched string and add it to the child so the Processor catches it. */
70+
let [attribute_string] = source.match(Processor.ONLY_RE) ?? [];
71+
child.prepend(new Text(attribute_string));
72+
73+
str = topElement.innerText;
74+
}
75+
76+
/**
77+
* If the element is a <p> and the text is *only* an attribute, it was used as a block attribute
78+
* and should be removed.
79+
*/
80+
if (child instanceof HTMLParagraphElement) {
81+
if (Processor.ONLY_RE.test(child.innerText.trim())) {
82+
child.detach();
83+
return;
84+
}
85+
}
86+
4687
/** Test if the element contains attributes. */
4788
if (!Processor.BASE_RE.test(str)) return;
4889

src/processor.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface ElementWithAttributes {
66

77
export default class Processor {
88
static BASE_RE = /\{\:[ ]*([^\}\n ][^\}\n]*)[ ]*\}/;
9+
static ONLY_RE = /^\{\:[ ]*([^\}\n ][^\}\n]*)[ ]*\}$/;
910
static BLOCK_RE = /\n[ ]*\{\:?[ ]*([^\}\n ][^\}\n]*)[ ]*\}[ ]*$/;
1011

1112
constructor(private topLevelElement: HTMLElement) {}
@@ -103,7 +104,6 @@ export default class Processor {
103104

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

108108
if (Processor.BLOCK_RE.test(text)) {
109109
// Attributes should apply to the whole block.
@@ -129,7 +129,6 @@ export default class Processor {
129129
}
130130
} else if (Processor.BASE_RE.test(text)) {
131131
// Attributes are inline.
132-
console.log(Processor.BASE_RE.test(text));
133132
// Get the text nodes that contains the attribute string.
134133
let textNode = Array.from(el.childNodes).find(
135134
(node) =>
@@ -147,10 +146,6 @@ export default class Processor {
147146

148147
// Collapsible elements are a special case due to the collapse handle.
149148
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-
);
154149
sibling = sibling.parentElement;
155150
}
156151

versions.json

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

0 commit comments

Comments
 (0)