[yaml-front-matter] initial commit (#43307)

This commit is contained in:
ZHAO Jinxiang 2020-03-23 23:49:57 +08:00 committed by GitHub
parent 7fce7c3827
commit 220266e22c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 0 deletions

42
types/yaml-front-matter/index.d.ts vendored Normal file
View File

@ -0,0 +1,42 @@
// Type definitions for yaml-front-matter 4.1
// Project: https://github.com/dworthen/js-yaml-front-matter#readme
// Definitions by: ZHAO Jinxiang <https://github.com/xiaoxiangmoe>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import { LoadOptions } from 'js-yaml';
export function loadFront(
content: string | Buffer,
options?: LoadOptions,
): {
readonly [key: string]: any;
readonly __content: string;
};
export function loadFront<contentKeyName extends string>(
content: string | Buffer,
options: LoadOptions & { contentKeyName: contentKeyName },
): {
readonly [key in contentKeyName]: string;
} & {
readonly [key: string]: any;
};
export function safeLoadFront(
content: string | Buffer,
options?: LoadOptions,
): {
readonly [key: string]: any;
readonly __content: string;
};
export function safeLoadFront<contentKeyName extends string>(
content: string | Buffer,
options: LoadOptions & { contentKeyName: contentKeyName },
): {
readonly [key in contentKeyName]: string;
} & {
readonly [key: string]: any;
};

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"yaml-front-matter-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@ -0,0 +1,25 @@
import * as yamlFront from 'yaml-front-matter';
const hello = yamlFront.loadFront('Hello World').__content;
const world = yamlFront.loadFront('Hello World', {
contentKeyName: 'fileContents'
}).fileContents;
const input = `---
post: title one
anArray:
- one
- two
subObject:
prop1: cool
prop2: two
reg: !!js/regexp /pattern/gim
fun: !!js/function function() { }
---
content
more`;
const results = yamlFront.loadFront(input);
console.log(JSON.stringify(results)); // "{"post":"title one","anArray":["one","two"],"subObject":{"obj1":"cool","obj2":"two"},"reg":{},"fun":[null],"__content":"\ncontent\nmore"}"