Support JSON front matter as used by the Hugo site generator (fixes #270).

This commit is contained in:
David Anson 2020-11-14 19:40:15 -08:00
parent 47d82d6fd4
commit bb157b2ce7
11 changed files with 60 additions and 7 deletions

View file

@ -429,11 +429,12 @@ ignores common forms of "front matter". To match differently, specify a custom
The default value: The default value:
```js ```js
/((^---\s*$[^]*?^---\s*$)|(^\+\+\+\s*$[^]*?^(\+\+\+|\.\.\.)\s*$))(\r\n|\r|\n|$)/m /((^---\s*$[^]*?^---\s*$)|(^\+\+\+\s*$[^]*?^(\+\+\+|\.\.\.)\s*$)|(^\{\s*$[^]*?^\}\s*$))(\r\n|\r|\n|$)/m
``` ```
Ignores [YAML](https://en.wikipedia.org/wiki/YAML) and Ignores [YAML](https://en.wikipedia.org/wiki/YAML),
[TOML](https://en.wikipedia.org/wiki/TOML) such as: [TOML](https://en.wikipedia.org/wiki/TOML), and
[JSON](https://en.wikipedia.org/wiki/JSON) front matter such as:
```text ```text
--- ---

View file

@ -848,7 +848,7 @@ Tags: headings, headers
Aliases: single-title, single-h1 Aliases: single-title, single-h1
Parameters: level, front_matter_title (number; default 1, string; default "^\s*title:") Parameters: level, front_matter_title (number; default 1, string; default "^\s*"?title"?\s*[:=]")
This rule is triggered when a top level heading is in use (the first line of This rule is triggered when a top level heading is in use (the first line of
the file is a h1 heading), and more than one h1 heading is in use in the the file is a h1 heading), and more than one h1 heading is in use in the
@ -1565,7 +1565,7 @@ Tags: headings, headers
Aliases: first-line-heading, first-line-h1 Aliases: first-line-heading, first-line-h1
Parameters: level, front_matter_title (number; default 1, string; default "^\s*title:") Parameters: level, front_matter_title (number; default 1, string; default "^\s*"?title"?\s*[:=]")
This rule is intended to ensure documents have a title and is triggered when This rule is intended to ensure documents have a title and is triggered when
the first line in the file isn't a top level (h1) heading: the first line in the file isn't a top level (h1) heading:

View file

@ -12,7 +12,7 @@ module.exports.newLineRe = newLineRe;
// Regular expression for matching common front matter (YAML and TOML) // Regular expression for matching common front matter (YAML and TOML)
module.exports.frontMatterRe = module.exports.frontMatterRe =
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
/((^---\s*$[^]*?^---\s*$)|(^\+\+\+\s*$[^]*?^(\+\+\+|\.\.\.)\s*$))(\r\n|\r|\n|$)/m; /((^---\s*$[^]*?^---\s*$)|(^\+\+\+\s*$[^]*?^(\+\+\+|\.\.\.)\s*$)|(^\{\s*$[^]*?^\}\s*$))(\r\n|\r|\n|$)/m;
// Regular expression for matching inline disable/enable comments // Regular expression for matching inline disable/enable comments
const inlineCommentRe = const inlineCommentRe =
@ -502,7 +502,10 @@ module.exports.frontMatterHasTitle =
const ignoreFrontMatter = const ignoreFrontMatter =
(frontMatterTitlePattern !== undefined) && !frontMatterTitlePattern; (frontMatterTitlePattern !== undefined) && !frontMatterTitlePattern;
const frontMatterTitleRe = const frontMatterTitleRe =
new RegExp(String(frontMatterTitlePattern || "^\\s*title\\s*[:=]"), "i"); new RegExp(
String(frontMatterTitlePattern || "^\\s*\"?title\"?\\s*[:=]"),
"i"
);
return !ignoreFrontMatter && return !ignoreFrontMatter &&
frontMatterLines.some((line) => frontMatterTitleRe.test(line)); frontMatterLines.some((line) => frontMatterTitleRe.test(line));
}; };

View file

@ -0,0 +1,6 @@
{
"default": true,
"MD041": {
"front_matter_title": "^\\s*\"alternate\"\\s*:"
}
}

View file

@ -0,0 +1,6 @@
{
"date": "2017-01-26T22:17:00+02:00",
"alternate": "My document title and heading"
}
Some plain text here.

View file

@ -0,0 +1,4 @@
{
"default": true,
"MD041": true
}

View file

@ -0,0 +1,6 @@
{
"date": "2017-01-26T22:17:00+02:00",
"title": "My document title and heading"
}
Some plain text here.

View file

@ -0,0 +1,4 @@
{
"default": true,
"MD041": true
}

View file

@ -0,0 +1,6 @@
{
"date": "2017-01-26T22:17:00+02:00",
"title": "My document title and heading"
}
Some plain text here.

View file

@ -0,0 +1,4 @@
{
"default": true,
"MD041": true
}

View file

@ -0,0 +1,13 @@
{
"title": "Another Hugo Post",
"description": "Nothing special, but one post is boring.",
"date": "2014-09-02",
"categories": [ "example", "configuration" ],
"tags": [
"example",
"hugo",
"toml"
],
}
# Heading {MD025}