Skip to content

Commit 87e7e5b

Browse files
committed
.
0 parents  commit 87e7e5b

10 files changed

+341
-0
lines changed

.editorconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
*.log
3+
.nyc_output/
4+
coverage/
5+
node_modules/
6+
yarn.lock

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage/

.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
os:
3+
- windows
4+
- linux
5+
node_js:
6+
- lts/dubnium
7+
- node
8+
after_script: bash <(curl -s https://codecov.io/bash)

index.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict'
2+
3+
var buffer = require('is-buffer')
4+
var yamlParse = require('js-yaml').safeLoad
5+
var parse = require('remark-frontmatter/lib/parse')
6+
var matters = require('remark-frontmatter/lib/matters')
7+
8+
module.exports = frontmatter
9+
10+
var matterParse = parse(matters('yaml')[0])[1]
11+
12+
function frontmatter(file, options) {
13+
var strip = (options || {}).strip
14+
var data = file.data
15+
var doc = String(file)
16+
var result = matterParse(mockEat, doc)
17+
var offset
18+
19+
data.frontmatter = {}
20+
21+
if (result) {
22+
data.frontmatter = yamlParse(result.value, {filename: file.path})
23+
24+
if (strip) {
25+
offset = result.length
26+
27+
// \n
28+
if (doc.charCodeAt(offset) === 10) {
29+
offset++
30+
}
31+
32+
doc = doc.slice(offset)
33+
file.contents = buffer(file.contents) ? Buffer.from(doc) : doc
34+
}
35+
}
36+
37+
return file
38+
}
39+
40+
function mockEat(doc) {
41+
return add
42+
function add(node) {
43+
return {length: doc.length, value: node.value}
44+
}
45+
}

license

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2019 Titus Wormer <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package.json

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"name": "vfile-frontmatter",
3+
"version": "0.0.0",
4+
"description": "Parse the YAML frontmatter in a vfile",
5+
"license": "MIT",
6+
"keywords": [
7+
"virtual",
8+
"file",
9+
"vfile",
10+
"frontmatter",
11+
"yaml"
12+
],
13+
"repository": "vfile/vfile-frontmatter",
14+
"bugs": "https://github.com/vfile/vfile-frontmatter/issues",
15+
"funding": {
16+
"type": "opencollective",
17+
"url": "https://opencollective.com/unified"
18+
},
19+
"author": "Titus Wormer <[email protected]> (https://wooorm.com)",
20+
"contributors": [
21+
"Titus Wormer <[email protected]> (https://wooorm.com)"
22+
],
23+
"main": "index.js",
24+
"files": [
25+
"index.js"
26+
],
27+
"dependencies": {
28+
"is-buffer": "^2.0.0",
29+
"remark-frontmatter": "^1.3.0"
30+
},
31+
"devDependencies": {
32+
"js-yaml": "^3.13.1",
33+
"nyc": "^14.0.0",
34+
"prettier": "^1.0.0",
35+
"remark-cli": "^7.0.0",
36+
"remark-preset-wooorm": "^6.0.0",
37+
"tape": "^4.0.0",
38+
"to-vfile": "^6.0.0",
39+
"xo": "^0.25.0"
40+
},
41+
"scripts": {
42+
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
43+
"test-api": "node test",
44+
"test-coverage": "nyc --reporter lcov tape test.js",
45+
"test": "npm run format && npm run test-coverage"
46+
},
47+
"nyc": {
48+
"check-coverage": true,
49+
"lines": 100,
50+
"functions": 100,
51+
"branches": 100
52+
},
53+
"prettier": {
54+
"tabWidth": 2,
55+
"useTabs": false,
56+
"singleQuote": true,
57+
"bracketSpacing": false,
58+
"semi": false,
59+
"trailingComma": "none"
60+
},
61+
"xo": {
62+
"prettier": true,
63+
"esnext": false,
64+
"rules": {
65+
"guard-for-in": "off",
66+
"unicorn/prefer-includes": "off"
67+
}
68+
},
69+
"remarkConfig": {
70+
"plugins": [
71+
"preset-wooorm"
72+
]
73+
}
74+
}

readme.md

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# vfile-frontmatter
2+
3+
[![Build][build-badge]][build]
4+
[![Coverage][coverage-badge]][coverage]
5+
[![Downloads][downloads-badge]][downloads]
6+
[![Size][size-badge]][size]
7+
[![Sponsors][sponsors-badge]][collective]
8+
[![Backers][backers-badge]][collective]
9+
[![Chat][chat-badge]][chat]
10+
11+
Parse the YAML frontmatter in a [`vfile`][vfile].
12+
13+
## Install
14+
15+
[npm][]:
16+
17+
```sh
18+
npm install vfile-frontmatter
19+
```
20+
21+
## Use
22+
23+
Say we have a file, `example.html`:
24+
25+
```html
26+
---
27+
title: Hello, world!
28+
---
29+
<p>Some more text</p>
30+
```
31+
32+
And our script, `example.js`, looks like so:
33+
34+
```js
35+
var vfile = require('to-vfile')
36+
var frontmatter = require('vfile-frontmatter')
37+
38+
var file = vfile.readSync('example.html')
39+
40+
frontmatter(file, {strip: true})
41+
42+
console.log(file.data)
43+
console.log(String(file))
44+
```
45+
46+
Now, running our script (`node example`) yields:
47+
48+
```js
49+
{ frontmatter: { title: 'Hello, world!' } }
50+
```
51+
52+
```html
53+
<p>Some more text</p>
54+
```
55+
56+
## API
57+
58+
### `frontmatter(file[, options])`
59+
60+
Parse the YAML frontmatter in a [`vfile`][vfile], and add it as
61+
`file.data.frontmatter`.
62+
63+
If no frontmatter is found in the file, nothing happens, except that
64+
`file.data.frontmatter` is set to an empty object (`{}`).
65+
66+
###### Parameters
67+
68+
* `file` ([`VFile`][vfile])
69+
— Virtual file
70+
* `options.strip` (`boolean`, default: `false`)
71+
— Remove the YAML frontmatter from the file
72+
73+
###### Returns
74+
75+
The given `file`.
76+
77+
## Contribute
78+
79+
See [`contributing.md`][contributing] in [`vfile/.github`][health] for ways to
80+
get started.
81+
See [`support.md`][support] for ways to get help.
82+
83+
This project has a [Code of Conduct][coc].
84+
By interacting with this repository, organisation, or community you agree to
85+
abide by its terms.
86+
87+
## License
88+
89+
[MIT][license] © [Titus Wormer][author]
90+
91+
<!-- Definitions -->
92+
93+
[build-badge]: https://img.shields.io/travis/vfile/vfile-frontmatter.svg
94+
95+
[build]: https://travis-ci.org/vfile/vfile-frontmatter
96+
97+
[coverage-badge]: https://img.shields.io/codecov/c/github/vfile/vfile-frontmatter.svg
98+
99+
[coverage]: https://codecov.io/github/vfile/vfile-frontmatter
100+
101+
[downloads-badge]: https://img.shields.io/npm/dm/vfile-frontmatter.svg
102+
103+
[downloads]: https://www.npmjs.com/package/vfile-frontmatter
104+
105+
[size-badge]: https://img.shields.io/bundlephobia/minzip/vfile-frontmatter.svg
106+
107+
[size]: https://bundlephobia.com/result?p=vfile-frontmatter
108+
109+
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
110+
111+
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
112+
113+
[collective]: https://opencollective.com/unified
114+
115+
[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg
116+
117+
[chat]: https://spectrum.chat/unified/vfile
118+
119+
[npm]: https://docs.npmjs.com/cli/install
120+
121+
[contributing]: https://github.com/vfile/.github/blob/master/contributing.md
122+
123+
[support]: https://github.com/vfile/.github/blob/master/support.md
124+
125+
[health]: https://github.com/vfile/.github
126+
127+
[coc]: https://github.com/vfile/.github/blob/master/code-of-conduct.md
128+
129+
[license]: license
130+
131+
[author]: https://wooorm.com
132+
133+
[vfile]: https://github.com/vfile/vfile

test.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict'
2+
3+
var test = require('tape')
4+
var buffer = require('is-buffer')
5+
var vfile = require('to-vfile')
6+
var frontmatter = require('.')
7+
8+
var matter = '---\nkey: value\nlist:\n - 1\n - 2\n---'
9+
var doc = 'Here is a document\nMore of the document\nOther lines\n'
10+
var both = matter + '\n' + doc
11+
12+
test('vfile-frontmatter', function(t) {
13+
var file = vfile({contents: both})
14+
15+
t.equal(frontmatter(file), file, 'should return the given file')
16+
17+
t.deepEqual(
18+
file.data,
19+
{frontmatter: {key: 'value', list: [1, 2]}},
20+
'should add data'
21+
)
22+
23+
file = frontmatter(vfile({contents: doc}))
24+
t.deepEqual(file.data, {frontmatter: {}}, 'should support no frontmatter')
25+
26+
file = frontmatter(vfile({contents: both}), {strip: true})
27+
t.deepEqual(String(file), doc, 'should strip frontmatter')
28+
29+
file = frontmatter(vfile({contents: matter}), {strip: true})
30+
t.deepEqual(String(file), '', 'should strip frontmatter completely')
31+
32+
file = frontmatter(vfile({contents: doc}), {strip: true})
33+
t.deepEqual(String(file), doc, 'should support no frontmatter w/ strip')
34+
35+
file = frontmatter(vfile({contents: Buffer.from(both)}), {strip: true})
36+
t.ok(buffer(file.contents), 'should supporting buffers')
37+
38+
file = frontmatter(vfile(), {strip: true})
39+
t.ok(file.contents === undefined, 'should supporting empties')
40+
41+
t.end()
42+
})

0 commit comments

Comments
 (0)