smartfm/ts/index.ts

54 lines
1.1 KiB
TypeScript
Raw Normal View History

2016-11-13 22:11:49 +00:00
import 'typings-global'
let grayMatter = require('gray-matter')
export type TFrontMatter = 'yaml' | 'json'
export interface IParsedFM {
2017-03-11 21:34:28 +00:00
data: any
content: string
orig: string
2016-11-13 22:11:49 +00:00
}
export interface ISmartfmContructorOptions {
2017-03-11 21:34:28 +00:00
fmType: TFrontMatter
2016-11-13 22:11:49 +00:00
}
/**
* class smartfm handles frontmatter
*/
export class Smartfm {
2017-03-11 21:34:28 +00:00
fmType: TFrontMatter
constructor(optionsArg: ISmartfmContructorOptions) {
this.fmType = optionsArg.fmType
}
/**
* add frontmatter to a string
*/
stringify(bodyString: string, frontmatterData: any) {
return stringify(bodyString, frontmatterData)
}
/**
* parse a string that has frontmatter attached, YAML notation
*/
parse(stringToParse: string): IParsedFM {
return parse(stringToParse)
}
2016-11-13 22:11:49 +00:00
}
/**
* parse a string that has frontmatter attached, YAML notation
*/
export let parse = (stringToParse: string): IParsedFM => {
2017-03-11 21:34:28 +00:00
return grayMatter(stringToParse)
2016-11-13 22:11:49 +00:00
}
/**
* add frontmatter to a string
*/
export let stringify = (bodyString: string, frontmatterData: any) => {
2017-03-11 21:34:28 +00:00
return grayMatter.stringify(bodyString, frontmatterData)
2016-11-13 22:11:49 +00:00
}