Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Dec 2, 2019
0 parents commit 87e7e5b
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage/
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js
os:
- windows
- linux
node_js:
- lts/dubnium
- node
after_script: bash <(curl -s https://codecov.io/bash)
45 changes: 45 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict'

var buffer = require('is-buffer')
var yamlParse = require('js-yaml').safeLoad
var parse = require('remark-frontmatter/lib/parse')
var matters = require('remark-frontmatter/lib/matters')

module.exports = frontmatter

var matterParse = parse(matters('yaml')[0])[1]

function frontmatter(file, options) {
var strip = (options || {}).strip
var data = file.data
var doc = String(file)
var result = matterParse(mockEat, doc)
var offset

data.frontmatter = {}

if (result) {
data.frontmatter = yamlParse(result.value, {filename: file.path})

if (strip) {
offset = result.length

// \n
if (doc.charCodeAt(offset) === 10) {
offset++
}

doc = doc.slice(offset)
file.contents = buffer(file.contents) ? Buffer.from(doc) : doc
}
}

return file
}

function mockEat(doc) {
return add
function add(node) {
return {length: doc.length, value: node.value}
}
}
22 changes: 22 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2019 Titus Wormer <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
74 changes: 74 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"name": "vfile-frontmatter",
"version": "0.0.0",
"description": "Parse the YAML frontmatter in a vfile",
"license": "MIT",
"keywords": [
"virtual",
"file",
"vfile",
"frontmatter",
"yaml"
],
"repository": "vfile/vfile-frontmatter",
"bugs": "https://github.com/vfile/vfile-frontmatter/issues",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/unified"
},
"author": "Titus Wormer <[email protected]> (https://wooorm.com)",
"contributors": [
"Titus Wormer <[email protected]> (https://wooorm.com)"
],
"main": "index.js",
"files": [
"index.js"
],
"dependencies": {
"is-buffer": "^2.0.0",
"remark-frontmatter": "^1.3.0"
},
"devDependencies": {
"js-yaml": "^3.13.1",
"nyc": "^14.0.0",
"prettier": "^1.0.0",
"remark-cli": "^7.0.0",
"remark-preset-wooorm": "^6.0.0",
"tape": "^4.0.0",
"to-vfile": "^6.0.0",
"xo": "^0.25.0"
},
"scripts": {
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"guard-for-in": "off",
"unicorn/prefer-includes": "off"
}
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
]
}
}
133 changes: 133 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# vfile-frontmatter

[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]

Parse the YAML frontmatter in a [`vfile`][vfile].

## Install

[npm][]:

```sh
npm install vfile-frontmatter
```

## Use

Say we have a file, `example.html`:

```html
---
title: Hello, world!
---
<p>Some more text</p>
```

And our script, `example.js`, looks like so:

```js
var vfile = require('to-vfile')
var frontmatter = require('vfile-frontmatter')

var file = vfile.readSync('example.html')

frontmatter(file, {strip: true})

console.log(file.data)
console.log(String(file))
```

Now, running our script (`node example`) yields:

```js
{ frontmatter: { title: 'Hello, world!' } }
```

```html
<p>Some more text</p>
```

## API

### `frontmatter(file[, options])`

Parse the YAML frontmatter in a [`vfile`][vfile], and add it as
`file.data.frontmatter`.

If no frontmatter is found in the file, nothing happens, except that
`file.data.frontmatter` is set to an empty object (`{}`).

###### Parameters

* `file` ([`VFile`][vfile])
— Virtual file
* `options.strip` (`boolean`, default: `false`)
— Remove the YAML frontmatter from the file

###### Returns

The given `file`.

## Contribute

See [`contributing.md`][contributing] in [`vfile/.github`][health] for ways to
get started.
See [`support.md`][support] for ways to get help.

This project has a [Code of Conduct][coc].
By interacting with this repository, organisation, or community you agree to
abide by its terms.

## License

[MIT][license] © [Titus Wormer][author]

<!-- Definitions -->

[build-badge]: https://img.shields.io/travis/vfile/vfile-frontmatter.svg

[build]: https://travis-ci.org/vfile/vfile-frontmatter

[coverage-badge]: https://img.shields.io/codecov/c/github/vfile/vfile-frontmatter.svg

[coverage]: https://codecov.io/github/vfile/vfile-frontmatter

[downloads-badge]: https://img.shields.io/npm/dm/vfile-frontmatter.svg

[downloads]: https://www.npmjs.com/package/vfile-frontmatter

[size-badge]: https://img.shields.io/bundlephobia/minzip/vfile-frontmatter.svg

[size]: https://bundlephobia.com/result?p=vfile-frontmatter

[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg

[backers-badge]: https://opencollective.com/unified/backers/badge.svg

[collective]: https://opencollective.com/unified

[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg

[chat]: https://spectrum.chat/unified/vfile

[npm]: https://docs.npmjs.com/cli/install

[contributing]: https://github.com/vfile/.github/blob/master/contributing.md

[support]: https://github.com/vfile/.github/blob/master/support.md

[health]: https://github.com/vfile/.github

[coc]: https://github.com/vfile/.github/blob/master/code-of-conduct.md

[license]: license

[author]: https://wooorm.com

[vfile]: https://github.com/vfile/vfile
42 changes: 42 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

var test = require('tape')
var buffer = require('is-buffer')
var vfile = require('to-vfile')
var frontmatter = require('.')

var matter = '---\nkey: value\nlist:\n - 1\n - 2\n---'
var doc = 'Here is a document\nMore of the document\nOther lines\n'
var both = matter + '\n' + doc

test('vfile-frontmatter', function(t) {
var file = vfile({contents: both})

t.equal(frontmatter(file), file, 'should return the given file')

t.deepEqual(
file.data,
{frontmatter: {key: 'value', list: [1, 2]}},
'should add data'
)

file = frontmatter(vfile({contents: doc}))
t.deepEqual(file.data, {frontmatter: {}}, 'should support no frontmatter')

file = frontmatter(vfile({contents: both}), {strip: true})
t.deepEqual(String(file), doc, 'should strip frontmatter')

file = frontmatter(vfile({contents: matter}), {strip: true})
t.deepEqual(String(file), '', 'should strip frontmatter completely')

file = frontmatter(vfile({contents: doc}), {strip: true})
t.deepEqual(String(file), doc, 'should support no frontmatter w/ strip')

file = frontmatter(vfile({contents: Buffer.from(both)}), {strip: true})
t.ok(buffer(file.contents), 'should supporting buffers')

file = frontmatter(vfile(), {strip: true})
t.ok(file.contents === undefined, 'should supporting empties')

t.end()
})

0 comments on commit 87e7e5b

Please sign in to comment.