@ 11ty/ gray- matter
减少和维持的叉子 gray-matter 原由乔恩·施林克特(Jon Schlinkert)。 。 。 .
当前差异 :
- 上流软件包最后一次发布于2019年。 。 。 .
- 升级
js-yaml向 v4 主要版本 - 内部使用 Uint8Array 而不是 Node- env 特定缓冲器来进行更多的运行时间相容性.
- 删除
javascript前事项类型( 取消对eval(中文(简体) )
从字符串或文件中解析前题 。 快速,可靠和易于使用. 默认情况下将 YAML 前置事项解析,但也支持 YAML, JSON, 或 TOML Front- Matter, 选项设置自定义分隔符 .
安装
安装方式 无数字 :
$ npm install --save @11ty/gray-matter
抬头!
请看 更改日志 来了解在v3.0中所做的改变.
这有什么用?
运行此示例
以下示例中的 HTML 添加到 example.html,然后将以下代码添加到 example.js 运行 $ node example (无 $:
const fs = require('fs');
const matter = require('@11ty/gray-matter');
const str = fs.readFileSync('example.html', 'utf8');
console.log(matter(str));
用前题转换字符串, 像这样 :
---
title: Hello
slug: home
---
<h1>Hello world!</h1>
进入这样的物体:
{
content: '<h1>Hello world!</h1>',
data: {
title: 'Hello',
slug: 'home'
}
}
为什么用灰色的东西?
- 简单: 主函数取字符串并返回对象
- 准确: 优于依赖regex进行解析的前题解析器来捕捉和处理边缘案件
- 快点: 比其它使用正则正则解析器进行解析的前事解析器更快
- 灵活: 默认情况下, 灰色物质能够解析 也门, (中文(简体) ), 贾森 和 JavaScript 前题。 但别的地方 发动机 可添加.
- 可扩展: 使用 自定义分隔符,或添加对 任何语言一样 移动。 。 。 .
- 作战测试: 由 组装, (中文(简体) ), 金属匠, (中文(简体) ), 花环, (中文(简体) ), 动词, (中文(简体) ), 生成, (中文(简体) ), 更新 还有其他许多.
理由
为什么我们一开始要创造出灰色的东西?
我们在尝试了其他不符合我们的标准和要求的图书馆之后创造了灰色的东西.
一些图书馆满足了大部分要求,但 没有人见过他们。 。 。 .
这是最重要的数字 :
- 可用,即使不简单
- 使用可靠且支持良好的库来解析 YAML
- 除YAML外支持其他语言
- 支持将字符串回YAML 或其他语言
- 内容不存在时不要失败
- 不要失败,没有前题
- 别用regex来解析. 这是一个相对简单的解析操作,而regex是最慢和最容易出错的方法.
- 直接读取 YAML 文件没有问题
- 内容复杂没有问题,包括: 非前事事项 包含 YAML 前置物质示例的有栅栏的代码块 。 其他分析失败了.
- 支持将字符串恢复到前题 。 这对折叠,更新属性等都有用.
- 允许自定义分界器,当它是避免分界器相撞所必需的.
- 返回至少具有这三种属性的对象时:
data:解析出 YAML 前题, 作为 JSON 对象content: 内容为字符串,没有前题orig:"原始"内容 (用于调试)
使用量
使用普通JS :
const matter = require('@11ty/gray-matter');
或无害环境管理:
import matter = require('@11ty/gray-matter');
// OR
import * as matter from '@11ty/gray-matter';
通过字符串和 选项 改为灰色物质 :
console.log(matter('---\ntitle: Front Matter\n---\nThis is content.'));
回返情况:
{
content: '\nThis is content.',
data: {
title: 'Front Matter'
}
}
更多关于下一节中返回的对象.
返回对象
灰色物质返回 a file 对象,其属性如下.
可假设
file.data{对象}: 通过解析前题创建的对象file.content{string}(string ) , [ 弦叫 ]: 输入字符串,与matter剥去file.excerpt{string}(string ) , [ 弦叫 ]: 节选,如果 在选项上定义file.empty{string}(string ) , [ 弦叫 ]:当前题为"空"时(要么全部为白空,完全没有,要么只是评论和没有数据),原始字符串设置在这个属性上. 见 65号 关于使用案例的细节.file.isEmpty(保罗): 前题为空时为真 .
不可假定
此外,在对象上还添加了以下非数字属性来帮助调试.
file.orig{Uint8箭头}: 原始输入字符串(或 Uint8Array)file.language{string}(string ) , [ 弦叫 ]:被解析的前题语言.yaml是默认file.matter{string}(string ) , [ 弦叫 ]页:1 原始内容未解析前事项字符串file.stringify{职能}数字 : 字符串 转换文件file.data以给定语言将字符串包入分隔符并预留到file.content。 。 。 .
运行示例
如果你想试一下这些例子 首先克隆出灰色物质 my-project (或随所取地):
$ git clone https://github.com/jonschlinkert/gray-matter my-project
CD 进入 my-project 并安装依赖性 :
$ cd my-project && npm install
然后运行任何 实例 以查看灰色物质如何工作:
$ node examples/<example_name>
与实例的链接
API 密码
事项
用字符串或对象 content 属性,从字符串中取出并解析前题,然后返回对象 data, (中文(简体) ), content 其他项目 有用的属性。 。 。 .
参数
input{对象}: 字符串,或对象content字符串options{对象}returns{对象}
示例
const matter = require('@11ty/gray-matter');
console.log(matter('---\ntitle: Home\n---\nOther stuff'));
//=> { data: { title: 'Home'}, content: 'Other stuff' }
字符串
将对象字符串为 YAML 或指定语言, 并附加到指定字符串中 。 默认情况下,只有YAML和JSON可以被字符串. 见 发动机 区域来学习如何将其他语言字符串化.
参数
file目标: 附加到字符串前事项的内容字符串, 或文件对象file.content绳子.data{对象}: 前题需要字符串.options{对象}数字 : 选项 传递到灰色物质和 日-亚姆尔。 。 。 .returns{string}(string ) , [ 弦叫 ]: 返回通过用分隔符将字符串相接而创建的字符串,并将它附加到给定字符串中.
示例
console.log(matter.stringify('foo bar baz', {title: 'Home'}));
// results in:
// ---
// title: Home
// ---
// foo bar baz
读取
同步读取文件系统的文件并解析前题 。 返回与 主要函数。 。 。 .
参数
filepath{string}(string ) , [ 弦叫 ]: 要读取的文件路径 .options{对象}数字 : 选项 传递到灰色的东西.returns{对象}: 回归 对象 与data财务报告和审定财务报表content
示例
const file = matter.read('./content/blog-post.md');
测试
如果给定返回真 string 有前题.
参数
string{string}(string ) , [ 弦叫 ]options{对象}returns(保罗): 如果前题存在, 则为真 .
选项
选项。节选
类型数字 : Boolean|Function
默认数字 : undefined
摘录一个直接跟随前事的节选,或者在没有前事的情况下是字符串中的第一件事.
如果设置为 excerpt: true它会寻找前题划界器, --- 默认,并抓住一切导致它.
示例
const str = '---\nfoo: bar\n---\nThis is an excerpt.\n---\nThis is content';
const file = matter(str, { excerpt: true });
成果:
{
content: 'This is an excerpt.\n---\nThis is content',
data: { foo: 'bar' },
excerpt: 'This is an excerpt.\n'
}
您也可以设置 excerpt 到一个函数。 此函数使用“ 文件” 和“ 选项” 作为参数, 以控制从内容中提取节选 .
示例
// returns the first 4 lines of the contents
function firstFourLines(file, options) {
file.excerpt = file.content.split('\n').slice(0, 4).join(' ');
}
const file = matter([
'---',
'foo: bar',
'---',
'Only this',
'will be',
'in the',
'excerpt',
'but not this...'
].join('\n'), {excerpt: firstFourLines});
成果:
{
content: 'Only this\nwill be\nin the\nexcerpt\nbut not this...',
data: { foo: 'bar' },
excerpt: 'Only this will be in the excerpt'
}
选项.节选 分隔符
类型数字 : String
默认数字 : undefined
定义用于节选的自定义分隔符 .
console.log(matter(string, {excerpt_separator: '<!-- end -->'}));
示例
以下 HTML 字符串 :
---
title: Blog
---
My awesome blog.
<!-- end -->
<h1>Hello world</h1>
成果:
{
data: { title: 'Blog'},
excerpt: 'My awesome blog.',
content: 'My awesome blog.\n<!-- end -->\n<h1>Hello world</h1>'
}
选项
定义用于解析和/或字符串前题的自定义引擎 .
类型数字 : Object 发动机对象
默认数字 : JSON, (中文(简体) ), YAML 财务报告和审定财务报表 JavaScript 默认已处理.
引擎格式
发动机可以是具有下列特性的物体: parse 和(可选) stringify 方法,或仅用于解析的函数.
实例
const toml = require('toml');
/**
* defined as a function
*/
const file = matter(str, {
engines: {
toml: toml.parse.bind(toml),
}
});
/**
* Or as an object
*/
const file = matter(str, {
engines: {
toml: {
parse: toml.parse.bind(toml),
// example of throwing an error to let users know stringifying is
// not supported (a TOML stringifier might exist, this is just an example)
stringify: function() {
throw new Error('cannot stringify to TOML');
}
}
}
});
console.log(file);
选项。语言
类型数字 : String
默认数字 : yaml
定义用于解析前题的引擎 .
console.log(matter(string, {language: 'toml'}));
示例
以下 HTML 字符串 :
---
title = "TOML"
description = "Front matter"
categories = "front matter toml"
---
This is content
成果:
{ content: 'This is content',
excerpt: '',
data:
{ title: 'TOML',
description: 'Front matter',
categories: 'front matter toml' } }
动态语言检测
灰色物质不会定义选项上的语言,而是会自动检测第一个分隔符后定义的语言并选择用于解析的正确引擎.
---toml
title = "TOML"
description = "Front matter"
categories = "front matter toml"
---
This is content
选项。界限
类型数字 : String
默认数字 : ---
打开和关闭的分隔符可以作为字符串的数组传递.
示例
// format delims as a string
matter.read('file.md', {delims: '~~~'});
// or an array (open/close)
matter.read('file.md', {delims: ['~~~', '~~~']});
将解析:
~~~
title: Home
~~~
This is the {{title}} page.
折旧选项
选项。lang
减号,请使用 选项。语言 换句话说.
选项.delims
减号,请使用 选项。界限 换句话说.
选项。参数
减号,请使用 选项 换句话说.
关于
捐款
拉出请求和星星总是受欢迎的. 对于错误和特性请求, 请创建一个问题。 。 。 .
运行测试
运行和审查单元测试是熟悉图书馆及其API的好办法。 您可以使用以下命令安装依赖性并运行测试:
$ npm install && npm test
贡献者
| 提交 | 贡献者 |
|---|---|
| 179国道 | 扬施林克特 |
| 第13条 | 罗伯特马萨约利 |
| 第7条 | 罗布洛克 |
| 页:1 | 鸽子 |
| 页:1 | {\fn黑体\fs22\bord1\shad0\3ahbe\4ah00\fscx67\fscy66\2chffffff\3ch808080}嘿 哥们 |
| 3个 | 极乐世界 |
| 3个 | 摇摆机器人 |
| 2个 | 矩形 |
| 2个 | 顶点 |
| 2个 | moo克语name |
| 2个 | 阿杰马瑟 |
| 1个 | 阿杰迪32号 |
| 1个 | 阿赖尔 |
| 1个 | 开萨 |
| 1个 | 扬·斯通克勒 |
| 1个 | 克立方米 |
| 1个 | 扎克瓦利语name |
作者
乔恩·斯林克特