Skip to content

Commit b17d514

Browse files
committed
feature: @putout/processor-markdown: add support frontmatter
1 parent e0f4cfc commit b17d514

File tree

5 files changed

+21
-46
lines changed

5 files changed

+21
-46
lines changed

packages/processor-markdown/lib/markdown.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import {toJS, fromJS} from '@putout/operator-json';
22
import stringify from 'remark-stringify';
33
import preset from 'remark-preset-lint-consistent';
4+
import remarkFrontmatter from 'remark-frontmatter';
45
import {visit} from 'unist-util-visit';
56
import {unified} from 'unified';
7+
import remarkParse from 'remark-parse';
68
import removeDependenciesStatusBadge from './rules/remove-dependencies-status-badge.js';
79
import removeTrailingWhitespacesFromHeading from './rules/remove-trailing-whitespaces-from-heading.js';
810
import mergeHeadingSpceces from './rules/merge-heading-spaces.js';
911
import {run} from './rules/index.js';
1012
import {toPlace} from './parse-place.js';
11-
import {initParseStore} from './parse-store.js';
1213

1314
const plugins = [
1415
removeDependenciesStatusBadge,
1516
removeTrailingWhitespacesFromHeading,
1617
mergeHeadingSpceces,
1718
];
1819

19-
const parseStore = initParseStore();
20-
2120
const text = ({value}) => value;
2221

2322
const stringifyOptions = {
@@ -32,13 +31,11 @@ const stringifyOptions = {
3231

3332
export const files = ['*.md'];
3433
export const find = async (rawSource, options = {}) => {
35-
await parseStore.init();
36-
3734
if (!rawSource.length)
3835
return [];
3936

4037
const {messages} = await unified()
41-
.use(parseStore)
38+
.use(remarkParse)
4239
.use(preset)
4340
.use(run, {
4441
fix: false,
@@ -48,24 +45,24 @@ export const find = async (rawSource, options = {}) => {
4845
],
4946
})
5047
.use(stringify, stringifyOptions)
48+
.use(remarkFrontmatter, ['yaml', 'toml'])
5149
.process(rawSource);
5250

5351
return messages.map(toPlace);
5452
};
5553
export const fix = async (rawSource, options = {}) => {
56-
await parseStore.init();
57-
5854
const {value} = await unified()
59-
.use(parseStore)
55+
.use(remarkParse)
6056
.use(preset)
57+
.use(stringify, stringifyOptions)
58+
.use(remarkFrontmatter, ['yaml', 'toml'])
6159
.use(run, {
6260
fix: true,
6361
plugins: [
6462
...plugins,
6563
...options.plugins || [],
6664
],
6765
})
68-
.use(stringify, stringifyOptions)
6966
.process(rawSource);
7067

7168
return value;
@@ -74,7 +71,7 @@ export const branch = async (rawSource) => {
7471
const list = [];
7572

7673
await unified()
77-
.use(parseStore)
74+
.use(remarkParse)
7875
.use(collect, {
7976
list,
8077
visit,
@@ -88,7 +85,7 @@ export const merge = async (rawSource, list) => {
8885
const newList = list.slice();
8986

9087
const {value} = await unified()
91-
.use(parseStore)
88+
.use(remarkParse)
9289
.use(apply, {
9390
list: newList,
9491
rawSource,
@@ -97,15 +94,12 @@ export const merge = async (rawSource, list) => {
9794
.use(stringify, stringifyOptions)
9895
.process(rawSource);
9996

100-
await parseStore.clear();
101-
10297
return value;
10398
};
10499

105100
const collect = ({list, visit}) => (node) => {
106101
visit(node, 'code', (node) => {
107102
const {lang, value} = node;
108-
109103
const startLine = node.position.start.line;
110104

111105
if (/^(js|javascript)$/.test(lang)) {

packages/processor-markdown/lib/parse-store.js

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

packages/processor-markdown/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/processor-markdown#readme",
88
"main": "./lib/markdown.js",
99
"exports": {
10-
".": "./lib/markdown.js",
11-
"./parse-store": "./lib/parse-store.js"
10+
".": "./lib/markdown.js"
1211
},
1312
"release": false,
1413
"tag": false,
@@ -31,6 +30,7 @@
3130
"dependencies": {
3231
"@putout/operator-json": "^2.0.0",
3332
"once": "^1.4.0",
33+
"remark-frontmatter": "^5.0.0",
3434
"remark-parse": "^11.0.0",
3535
"remark-preset-lint-consistent": "^6.0.0",
3636
"remark-stringify": "^11.0.0",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: needs clarification
6+
---

packages/processor-markdown/test/markdown.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,7 @@ test('putout: processor: markdown: merge-heading-spaces: comparePlaces', async (
155155
test('putout: processor: markdown: compare places: empty', async ({comparePlaces}) => {
156156
await comparePlaces('empty', []);
157157
});
158+
159+
test('putout: processor: markdown: compare places: frontmatter', async ({noProcess}) => {
160+
await noProcess('frontmatter');
161+
});

0 commit comments

Comments
 (0)