diff --git a/types/yaml-front-matter/index.d.ts b/types/yaml-front-matter/index.d.ts new file mode 100644 index 0000000000..05cdde4e97 --- /dev/null +++ b/types/yaml-front-matter/index.d.ts @@ -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 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import { LoadOptions } from 'js-yaml'; + +export function loadFront( + content: string | Buffer, + options?: LoadOptions, +): { + readonly [key: string]: any; + readonly __content: string; +}; + +export function loadFront( + 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( + content: string | Buffer, + options: LoadOptions & { contentKeyName: contentKeyName }, +): { + readonly [key in contentKeyName]: string; +} & { + readonly [key: string]: any; +}; diff --git a/types/yaml-front-matter/tsconfig.json b/types/yaml-front-matter/tsconfig.json new file mode 100644 index 0000000000..f22c8b1e47 --- /dev/null +++ b/types/yaml-front-matter/tsconfig.json @@ -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" + ] +} diff --git a/types/yaml-front-matter/tslint.json b/types/yaml-front-matter/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/yaml-front-matter/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/yaml-front-matter/yaml-front-matter-tests.ts b/types/yaml-front-matter/yaml-front-matter-tests.ts new file mode 100644 index 0000000000..a156bcfc29 --- /dev/null +++ b/types/yaml-front-matter/yaml-front-matter-tests.ts @@ -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"}"