remark-frontmatter
Advanced tools
Comparing version 1.1.0 to 1.2.0
'use strict'; | ||
var fence = require('./fence'); | ||
module.exports = create; | ||
@@ -7,4 +9,4 @@ | ||
var type = matter.type; | ||
var marker = matter.marker; | ||
var fence = marker + marker + marker; | ||
var open = fence(matter, 'open'); | ||
var close = fence(matter, 'close'); | ||
@@ -16,4 +18,4 @@ frontmatter.displayName = type + 'FrontMatter'; | ||
function frontmatter(node) { | ||
return fence + (node.value ? '\n' + node.value : '') + '\n' + fence; | ||
return open + (node.value ? '\n' + node.value : '') + '\n' + close; | ||
} | ||
} |
@@ -50,4 +50,4 @@ 'use strict'; | ||
if (!own.call(result, 'marker')) { | ||
throw fault('Missing `marker` in matter `%j`', result); | ||
if (!own.call(result, 'fence') && !own.call(result, 'marker')) { | ||
throw fault('Missing `marker` or `fence` in matter `%j`', result); | ||
} | ||
@@ -54,0 +54,0 @@ |
'use strict'; | ||
var fence = require('./fence'); | ||
module.exports = create; | ||
@@ -7,4 +9,4 @@ | ||
var name = matter.type + 'FrontMatter'; | ||
var marker = matter.marker; | ||
var fence = marker + marker + marker; | ||
var open = fence(matter, 'open'); | ||
var close = fence(matter, 'close'); | ||
var newline = '\n'; | ||
@@ -18,52 +20,28 @@ | ||
function frontmatter(eat, value, silent) { | ||
var subvalue; | ||
var content; | ||
var index; | ||
var length; | ||
var character; | ||
var queue; | ||
var index = open.length; | ||
var offset; | ||
if ( | ||
value.charAt(0) !== marker || | ||
value.charAt(1) !== marker || | ||
value.charAt(2) !== marker || | ||
value.charAt(3) !== newline | ||
) { | ||
if (value.slice(0, index) !== open || value.charAt(index) !== newline) { | ||
return; | ||
} | ||
subvalue = fence + newline; | ||
content = ''; | ||
queue = ''; | ||
index = 3; | ||
length = value.length; | ||
offset = value.indexOf(close, index); | ||
while (++index < length) { | ||
character = value.charAt(index); | ||
while (offset !== -1 && value.charAt(offset - 1) !== newline) { | ||
index = offset + close.length; | ||
offset = value.indexOf(close, index); | ||
} | ||
if ( | ||
character === marker && | ||
(queue || !content) && | ||
value.charAt(index + 1) === marker && | ||
value.charAt(index + 2) === marker | ||
) { | ||
/* istanbul ignore if - never used (yet) */ | ||
if (silent) { | ||
return true; | ||
} | ||
subvalue += queue + fence; | ||
return eat(subvalue)({type: matter.type, value: content}); | ||
if (offset !== -1) { | ||
/* istanbul ignore if - never used (yet) */ | ||
if (silent) { | ||
return true; | ||
} | ||
if (character === newline) { | ||
queue += character; | ||
} else { | ||
subvalue += queue + character; | ||
content += queue + character; | ||
queue = ''; | ||
} | ||
return eat(value.slice(0, offset + close.length))({ | ||
type: matter.type, | ||
value: value.slice(open.length + 1, offset - 1) | ||
}); | ||
} | ||
} | ||
} |
{ | ||
"name": "remark-frontmatter", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Frontmatter (yaml, toml, and more) support for remark", | ||
@@ -17,3 +17,4 @@ "license": "MIT", | ||
"contributors": [ | ||
"Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)" | ||
"Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
"Max Kueng <me@maxkueng.com>" | ||
], | ||
@@ -20,0 +21,0 @@ "files": [ |
@@ -85,3 +85,3 @@ # remark-frontmatter [![Build Status][build-badge]][build-status] [![Coverage Status][coverage-badge]][coverage-status] [![Chat][chat-badge]][chat] | ||
###### `options` | ||
##### `options` | ||
@@ -91,3 +91,3 @@ One [`preset`][preset] or [`Matter`][matter], or an array of them, defining all | ||
###### `preset` | ||
##### `preset` | ||
@@ -99,9 +99,92 @@ Either `'yaml'` or `'toml'`: | ||
###### `Matter` | ||
##### `Matter` | ||
An object with a `type` and a `marker`: | ||
An object with a `type` and either a `marker` or a `fence`: | ||
* `type` (`string`) — Node type to parse to in [mdast][] and compile from | ||
* `marker` (`string`) — Character used for fences | ||
* `marker` (`string` or `{open: string, close: string}`) — Character used | ||
to construct fences. By providing an object with `open` and `close`. | ||
different characters can be used for opening and closing fences. For | ||
example the character `'-'` will result in `'---'` being used as the fence | ||
* `fence` (`string` or `{open: string, close: string}`) — String used as | ||
the complete fence. By providing an object with `open` and `close` | ||
different values can be used for opening and closing fences. This can be | ||
used too if fences contain different characters or lengths other than 3 | ||
###### Example | ||
For `{type: 'yaml', marker: '-'}`: | ||
```yaml | ||
--- | ||
key: value | ||
--- | ||
``` | ||
Yields: | ||
```json | ||
{ | ||
"type": "yaml", | ||
"value": "key: value" | ||
} | ||
``` | ||
###### Example | ||
For `{type: 'custom', marker: {open: '<', close: '>'}}`: | ||
```text | ||
<<< | ||
data | ||
>>> | ||
``` | ||
Yields: | ||
```json | ||
{ | ||
"type": "custom", | ||
"value": "data" | ||
} | ||
``` | ||
###### Example | ||
For `{type: 'custom', fence: '+=+=+=+'}`: | ||
```text | ||
+=+=+=+ | ||
data | ||
+=+=+=+ | ||
``` | ||
Yields: | ||
```json | ||
{ | ||
"type": "custom", | ||
"value": "dats" | ||
} | ||
``` | ||
###### Example | ||
For `{type: 'json', fence: {open: '{', close: '}'}}`: | ||
```json | ||
{ | ||
"key": "value" | ||
} | ||
``` | ||
Yields: | ||
```json | ||
{ | ||
"type": "json", | ||
"value": "\"key\": \"value\"" | ||
} | ||
``` | ||
## Related | ||
@@ -108,0 +191,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12328
8
239
157