Skip to content

Commit 9bf6de6

Browse files
authored
Refactor to unist-util-select (chrisg86#3)
* Refactor to unist-util-select * Version bump, next major with breaking changes * More examples * Fix class name * Update classMap section
1 parent 005687a commit 9bf6de6

File tree

4 files changed

+74
-44
lines changed

4 files changed

+74
-44
lines changed

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# gatsby-remark-classes
2+
23
Automatically add class attributes to markdown elements.
34

45
This is a plugin for [gatsby-transformer-remark](https://www.gatsbyjs.org/packages/gatsby-transformer-remark/?=gatsby-transformer-remark).
56

67
## Install
8+
79
```
810
npm install --save gatsby-remark-classes
911
```
1012

1113
## Configure
14+
1215
In your `gatsby-config.js`:
1316

1417
```js
@@ -20,9 +23,9 @@ In your `gatsby-config.js`:
2023
resolve: `gatsby-remark-classes`,
2124
options: {
2225
classMap: {
23-
h1: 'title',
24-
h2: 'subtitle',
25-
paragraph: 'para'
26+
"heading[depth=1]": "title",
27+
"heading[depth=2]": "subtitle",
28+
paragraph: "para",
2629
}
2730
}
2831
}
@@ -35,27 +38,47 @@ The rules above applied to the following markdown
3538

3639
```markdown
3740
# Main heading
41+
3842
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eum, odio.
3943

4044
## Sub header
45+
4146
Lorem ipsum dolor sit amet, consectetur adipisicing.
4247
```
4348

4449
Will result in this HTML output:
4550

4651
```html
4752
<h1 class="title">Main heading</h1>
48-
<p class="para">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eum, odio.</p>
53+
<p class="para">
54+
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eum, odio.
55+
</p>
4956

5057
<h2 class="subtitle">Sub header</h2>
5158
<p class="para">Lorem ipsum dolor sit amet, consectetur adipisicing.</p>
5259
```
5360

5461
## The classMap
55-
Right now you can add styles for **heading** elements (h1 - h6), **paragraphs** and **tables**.
62+
63+
For supported selectors please please consult [syntax-tree/mdast](https://github.com/syntax-tree/mdast#nodes) for the node list and have a look at the [Support section of unist-util-select](https://github.com/syntax-tree/unist-util-select#support)
5664

5765
## Motivation
5866

5967
Applying custom styles is also possible by just wrapping your converted markdown in a parent element and write the styles for that. This will however not work if you use atomic css in your project or a framework like Semantic UI or Tailwind CSS.
6068

6169
With this project you define which classes get assigned to which element.
70+
71+
## Upgrade guide
72+
73+
When upgrading from version 0.x.x to 1.x.x, you will have to update the selectors in your `gatsby-config.js` file.
74+
75+
Some common selectors:
76+
77+
- `h1` is now `heading[depth=1]`
78+
- `h2` is now `heading[depth=2]`
79+
- `ul` is now `list[ordered=false]`
80+
- `ol` is now `list[ordered=true]`
81+
- `li` is now `listItem`
82+
- `paragraph` is still `paragraph`
83+
84+
Additionally you have now the chance to target child elements `code > pre` or even adjacent elements `paragraph + paragraph`. As stated above, please consult [syntax-tree/mdast](https://github.com/syntax-tree/mdast#nodes) for the node list and [syntax-tree/unist-util-select](https://github.com/syntax-tree/unist-util-select#support) for pseudo selectors and syntax.

index.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const visit = require('unist-util-visit');
1+
const { selectAll } = require('unist-util-select');
22

33
const applyClassesToNode = (node, classes) => {
44
node.data = node.data || {};
@@ -11,28 +11,11 @@ const applyClassesToNode = (node, classes) => {
1111
};
1212

1313
module.exports = ({ markdownAST }, { classMap = {} }) => {
14-
// @see: https://github.com/syntax-tree/mdast#nodes
15-
visit(markdownAST, `heading`, (node) => {
16-
const selector = `h${node.depth}`;
14+
Object.keys(classMap).forEach((selector) => {
15+
const nodes = selectAll(selector, markdownAST);
1716

18-
if (selector in classMap) {
17+
nodes.forEach((node) => {
1918
node = applyClassesToNode(node, classMap[selector]);
20-
}
21-
});
22-
23-
visit(markdownAST, `table`, (node) => {
24-
const selector = `table`;
25-
26-
if (selector in classMap) {
27-
node = applyClassesToNode(node, classMap[selector]);
28-
}
29-
});
30-
31-
visit(markdownAST, `paragraph`, (node) => {
32-
const selector = `paragraph`;
33-
34-
if (selector in classMap) {
35-
node = applyClassesToNode(node, classMap[selector]);
36-
}
19+
});
3720
});
3821
};

package-lock.json

Lines changed: 39 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gatsby-remark-classes",
3-
"version": "0.1.2",
3+
"version": "1.0.0",
44
"description": "Automatically add class attributes to rendered markdown elements",
55
"main": "index.js",
66
"keywords": [
@@ -10,7 +10,7 @@
1010
"author": "Chris Greindl <[email protected]>",
1111
"license": "MIT",
1212
"dependencies": {
13-
"unist-util-visit": "^1.4.0"
13+
"unist-util-select": "^2.0.2"
1414
},
1515
"repository": {
1616
"type": "git",

0 commit comments

Comments
 (0)