Skip to content

Commit d0b55be

Browse files
committed
Update remark-attributes to use [[.some-class #my-id arbitrary=attribute]] syntax instead of {...} syntax that conflicts with Astro's embedded JS code feature in Astro templates
1 parent ce32410 commit d0b55be

10 files changed

Lines changed: 145 additions & 259 deletions

File tree

‎_TODO.md‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,28 +235,17 @@ You can then opt-out of prefetching for individual links by setting data-astro-p
235235

236236
- color tabs like GFM when using HEX, RGB, or HSL values in backticks. This should generate a callout box around the hex color with a dot to the right showing the color.
237237

238-
- Astro also includes shiki
239-
240-
- We're adding 'rehype-autolink-headings', but Astro does too: https://docs.astro.build/en/guides/markdown-content/#heading-ids-and-plugins
241-
242-
- `remark-attributes` does not work because `{...}` in MDX embeds dynamic JavaScript logic or JSX components directly within Markdown content
238+
The background color is `#ffffff`
243239

244-
Must define escaping rules (what if the content legitimately includes `[[...]]`), and whether .md, .mdx, or both:
240+
<code class="bg-gray-100 rounded border border-gray-300 inline-block font-mono text-xs mx-1 px-2 py-1">#ffffff</code>
245241

246-
```markdown
247-
### Heading [[.my-class #my-id]]
248-
![Image](url) [[width=300 .centered]]
249-
[Link](url) [[.btn target=_blank]]
242+
- Astro also includes shiki
250243

251-
### Heading[[.my-class #my-id]]
252-
![Image](url)[[width=300 .centered]]
253-
[Link](url)[[.btn target=_blank]]
254-
```
244+
- We're adding 'rehype-autolink-headings', but Astro does too: https://docs.astro.build/en/guides/markdown-content/#heading-ids-and-plugins
255245

256246
### Youtube embedding
257247

258-
- `rehype-video` for self-hosted videos, you have to pass the video filename into the unified `process()` pipeline along with the plugin
259-
- `@hongvanpc10/rehype-embed` works the same way - you have to pass the Youtube link into the config to be processed
248+
- `remark-video`
260249

261250

262251

‎src/content/articles/demo/index.mdx‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,16 +583,17 @@ Block elements can be wrapped with their own tags to apply `flex flex-col items-
583583

584584
### remark-attributes
585585

586-
@TODO: DOES NOT WORK
587-
588-
Add HTML attributes to any Markdown element using curly brace syntax:
586+
Add HTML attributes to supported Markdown elements using a MDX-safe doubled-square-bracket postfix syntax:
589587

590588
```markdown
591-
### Heading {.my-class #my-id}
592-
![Image](url){width=300 .centered}
593-
[Link](url){.btn target=_blank}
589+
### Heading [[.my-class #my-id]]
590+
![Image](/assets/images/shiprock.svg) [[width=300 .centered]]
591+
[Link](url) [[.btn target=_blank]]
594592
```
595593

594+
![Image](/assets/images/shiprock.svg) [[width=300 .centered]]
595+
[Link Example](/remark-attributes) [[.btn target=_blank]]
596+
596597
### remark-attribution
597598

598599
The plugin allows to provide an attribution for a quotation:

‎src/lib/markdown/__fixtures__/attributes.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
title: Attributes Test
33
---
44

5-
# Heading with Class{.custom-heading}
5+
# Heading with Class[[.custom-heading]]
66

7-
This is a paragraph with a [link with class](https://example.com){.custom-link}.
7+
This is a paragraph with a [link with class](https://example.com)[[.custom-link]].
88

9-
Inline `code with class`{.custom-code} should work too.
9+
Inline `code with class`[[.custom-code]] should work too.

‎src/lib/markdown/__fixtures__/full-pipeline.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The HTML and CSS specifications work together.
1212

1313
## Attributes
1414

15-
[This link has a class](https://example.com){.custom-link}
15+
[This link has a class](https://example.com)[[.custom-link]]
1616

1717
## Attribution
1818

‎src/lib/markdown/__tests__/integration/remark-attributes-astro.spec.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('remark-attributes (Layer 2: With Astro Pipeline)', () => {
1010
// To add attributes to tables, they must be in a separate paragraph.
1111

1212
it('should work with GFM strikethrough', async () => {
13-
const markdown = '[~~deleted text~~]{.highlight}'
13+
const markdown = '[~~deleted text~~][[.highlight]]'
1414

1515
const html = await processWithAstroSettings({
1616
markdown,
@@ -24,7 +24,7 @@ describe('remark-attributes (Layer 2: With Astro Pipeline)', () => {
2424

2525
it('should work with GFM task lists', async () => {
2626
const markdown = `
27-
- [ ] Task 1{.todo-item}
27+
- [ ] Task 1[[.todo-item]]
2828
- [x] Task 2
2929
`.trim()
3030

@@ -42,7 +42,7 @@ describe('remark-attributes (Layer 2: With Astro Pipeline)', () => {
4242
describe('attributes with Astro footnote settings', () => {
4343
it('should add attributes to footnote references', async () => {
4444
const markdown = `
45-
Text with footnote[^1]{.footnote-ref}.
45+
Text with footnote[^1][[.footnote-ref]].
4646
4747
[^1]: Footnote content
4848
`.trim()
@@ -60,7 +60,7 @@ Text with footnote[^1]{.footnote-ref}.
6060

6161
describe('edge cases with Astro settings', () => {
6262
it('should handle attributes on links with autolinks', async () => {
63-
const markdown = '[Visit](https://example.com){.external target=_blank}'
63+
const markdown = '[Visit](https://example.com)[[.external target=_blank]]'
6464

6565
const html = await processWithAstroSettings({
6666
markdown,
@@ -74,7 +74,7 @@ Text with footnote[^1]{.footnote-ref}.
7474
})
7575

7676
it('should preserve attributes through remarkRehype conversion', async () => {
77-
const markdown = '# Heading{.title #main-heading data-level=1}'
77+
const markdown = '# Heading[[.title #main-heading data-level=1]]'
7878

7979
const html = await processWithAstroSettings({
8080
markdown,

‎src/lib/markdown/plugins/rehype-tailwind/REFACTORING_SUMMARY.md‎

Lines changed: 0 additions & 158 deletions
This file was deleted.

‎src/lib/markdown/plugins/remark-attributes/README.md‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@ This plugin adds support for custom attributes to Markdown syntax.
55
For **security reasons**, this plugin uses [html-element-attributes](https://github.com/wooorm/html-element-attributes).
66
The use of JavaScript attributes (`onload` for example) is not allowed by default.
77

8-
## Default Syntax
8+
## Default Syntax (MDX-safe)
9+
10+
This project uses a doubled-square-bracket postfix marker instead of `{...}` (curly braces are reserved by MDX):
11+
12+
`[[.class #id key=value]]`
913

1014
### Images
1115

1216
~~~markdown
13-
![alt](img){attrs} / ![alt](img){ height=50 }
17+
![alt](img)[[attrs]] / ![alt](img)[[ height=50 ]]
1418
~~~
1519

1620
### Links
1721

1822
~~~markdown
19-
[rms with a computer](https://rms.sexy){rel="external"}
23+
[rms with a computer](https://rms.sexy)[[rel="external"]]
2024
~~~
2125

2226
### Autolink
@@ -29,11 +33,11 @@ Email me at : <mailto:falseEmail@example.org>
2933

3034
~~~markdown
3135
### This is a title
32-
{style="color:red;"}
36+
[[style="color:red;"]]
3337

3438
or
3539

36-
### This is a title {style="color:yellow;"}
40+
### This is a title [[style="color:yellow;"]]
3741

3842
If option enableAtxHeaderInline is set to `true` (default value).
3943
~~~
@@ -43,37 +47,37 @@ If option enableAtxHeaderInline is set to `true` (default value).
4347
~~~markdown
4448
This is a title
4549
---------------
46-
{style="color: pink;"}
50+
[[style="color: pink;"]]
4751
~~~
4852

4953
### Emphasis
5054

5155
~~~markdown
52-
Npm stand for *node*{style="color:red"} packet manager.
56+
Npm stand for *node*[[style="color:red"]] packet manager.
5357
~~~
5458

5559
### Strong
5660

5761
~~~markdown
58-
This is a **Unicorn**{awesome} !
62+
This is a **Unicorn**[[awesome]] !
5963
~~~
6064

6165
### Delete
6266

6367
~~~markdown
64-
Your problem is ~~at line 18~~{style="color: grey"}. My mistake, it's at line 14.
68+
Your problem is ~~at line 18~~[[style="color: grey"]]. My mistake, it's at line 14.
6569
~~~
6670

6771
### Code
6872

6973
~~~markdown
70-
You can use the `fprintf`{language=c} function to format the output to a file.
74+
You can use the `fprintf`[[language=c]] function to format the output to a file.
7175
~~~
7276

7377
### Footnote (using [remark-footnotes](https://github.com/remarkjs/remark-footnotes))
7478

7579
~~~markdown
76-
This is a footnote[^ref]{style="opacity: 0.8;"}
80+
This is a footnote[^ref][[style="opacity: 0.8;"]]
7781

7882

7983
[^ref]: And the reference.

0 commit comments

Comments
 (0)