-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.md
202 lines (147 loc) · 5.6 KB
/
README.md
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# rehype-semantic-blockquotes
A **[rehype][]** plugin to extend blockquote syntax to make it simple to mention/cite sources in a semantically correct way.
## Contents
- [What is this?](#what-is-this)
- [When should I use this?](#when-should-i-use-this)
- [Install](#install)
- [Use](#use)
- [API](#api)
- [Security](#security)
- [License](#license)
## What is this?
This package is a [unified][] ([rehype][]) plugin to extend blockquote syntax to allow simple citation/mention of source from which the quote originates conforming to semantic HTML standards
## When should I use this?
This project is useful if you want to have a simple syntax for citations in your blockquotes.
In markdown we can create blockquotes such as:
```md
> We cannot solve our problems with the same thinking we used when we created them.
```
But often times, it may be desireable to include a reference to the person that mentioned that quote.
We could use the `<cite>` element:
```md
> We cannot solve our problems with the same thinking we used when we created them.
> -- <cite>Albert Einstein</cite>
```
But that is _semantically incorrect_! [A `<cite>` element should refer to _work_](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/cite#usage_notes) and not _people_. (e.g. instagram post, book, article)
Additionally, putting a `<cite>` element within a `<blockquote>` is [forbidden by the HTML spec](https://www.w3.org/TR/html5-author/the-blockquote-element.html#the-blockquote-element) because it would make the citation a part of the quote.
So another solution could be something like this:
```md
> We cannot solve our problems with the same thinking we used when we created them.
-- Albert Einstein
```
But that feels wrong, because it would render in the following way:
```html
<blockquote>
<p>
We cannot solve our problems with the same thinking we used when we created
them.
</p>
</blockquote>
<p>-- Albert Einstein</p>
```
If we want to style them together we would have to wrap them within a parent element.
But there is a different approach, using the `<figure>` element we can create a more semantic version.
This plugin does just that.
Then we can easily style the blockquote and the caption however we want to using CSS
```css
[data-blockquote-container] {
display: flex;
flex-direction: column;
flex-gap: 8px;
}
[data-blockquote-credit]::before {
content: "- ";
}
```
## Install
This package is [ESM only][esm]. In Node.js (version 16+), install with [npm][]:
```
npm install rehype-semantic-blockquotes
```
## Use
Say we have the following file `example.js`:
```js
import rehypeFormat from "rehype-format";
import rehypeSemanticBlockquotes from "rehype-semantic-blockquotes";
import rehypeStringify from "rehype-stringify";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import { unified } from "unified";
const doc = `
> Better to admit you walked through the wrong door than spend your life in the wrong room.
>
> @ [Josh Davis](https://somewhere.com) <a href="https://somewhere.com"></a>
`;
const file = String(
await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeSemanticBlockquotes)
.use(rehypeStringify)
.use(rehypeFormat) // for demonstration purposes only
.process(doc),
);
console.log(file);
```
...then running `node example.js` yields:
```html
<figure data-blockquote-contaienr="">
<blockquote data-blockquote-content="">
<p>
Better to admit you walked through the wrong door than spend your life in
the wrong room.
</p>
</blockquote>
<figcaption data-blockquote-credit="">
<p><a href="https://somewhere.com">Josh Davis</a></p>
</figcaption>
</figure>
```
## API
This package exports no identifiers. The default export is `rehypeSemanticBlockquotes`.
#### `unified().use(rehypeSemanticBlockquotes)`
Adds syntax `@ ` which places the contents in the `@ ` into the `<figcaption>` element
###### Parameters
The attributes (`data-blockquote-figure`, etc.) are fully customizable. The plugin takes a parameter, `opts` with the following defaults:
```js
{
figure: "data-blockquote-contaienr",
blockquote: "data-blockquote-content",
figcaption: "data-blockquote-credit",
syntax: "@ ",
};
```
###### Returns
Transform ([`Transformer`][unified-transformer]).
#### Syntax Info
In the MD blockquote, if the last line starts with an `@ ` and the line before is an empty line then the transformation will take place. Otherwise we will just get a regular `<blockquote>`, the plugin won't take effect.
For example these snippets will not be affected by the plugin:
```md
> We cannot solve our problems with the same thinking we used when we created them.
```
```md
> We cannot solve our problems with the same thinking we used when we created them.
> @ Albert Einstein
```
But this would:
```md
> We cannot solve our problems with the same thinking we used when we created them.
>
> @ Albert Einstein
```
## Security
Use of `rehype-semantic-blockquotes` does not involve **[rehype][]** (**[hast][]**) or user
content so there are no openings for [cross-site scripting (XSS)][wiki-xss]
attacks.
## License
[MIT][license] © Nikita Revenco
[npm]: https://docs.npmjs.com/cli/install
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[license]: license
[hast]: https://github.com/syntax-tree/hast
[rehype]: https://github.com/rehypejs/rehype
[remark]: https://github.com/remarkjs/remark
[unified]: https://github.com/unifiedjs/unified
[unified-transformer]: https://github.com/unifiedjs/unified#transformer
[wiki-xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
[api-remark-unlink]: #unifieduseremarkvideos