2019-09-04 12:13:42 +00:00
|
|
|
import grayMatter from 'gray-matter';
|
2016-11-13 22:11:49 +00:00
|
|
|
|
2018-08-27 00:01:18 +00:00
|
|
|
export type TFrontMatter = 'yaml' | 'json';
|
2016-11-13 22:11:49 +00:00
|
|
|
|
|
|
|
export interface ISmartfmContructorOptions {
|
2018-08-27 00:01:18 +00:00
|
|
|
fmType: TFrontMatter;
|
2016-11-13 22:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* class smartfm handles frontmatter
|
|
|
|
*/
|
|
|
|
export class Smartfm {
|
2018-08-27 00:01:18 +00:00
|
|
|
fmType: TFrontMatter;
|
2017-03-11 21:34:28 +00:00
|
|
|
|
|
|
|
constructor(optionsArg: ISmartfmContructorOptions) {
|
2018-08-27 00:01:18 +00:00
|
|
|
this.fmType = optionsArg.fmType;
|
2017-03-11 21:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add frontmatter to a string
|
|
|
|
*/
|
|
|
|
stringify(bodyString: string, frontmatterData: any) {
|
2018-08-27 00:01:18 +00:00
|
|
|
return grayMatter.stringify(bodyString, frontmatterData);
|
2017-03-11 21:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parse a string that has frontmatter attached, YAML notation
|
|
|
|
*/
|
2018-08-27 00:01:18 +00:00
|
|
|
parse(stringToParse: string) {
|
|
|
|
return grayMatter(stringToParse);
|
2017-03-11 21:34:28 +00:00
|
|
|
}
|
2019-09-04 13:45:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* parse from commnets
|
|
|
|
*/
|
|
|
|
parseFromComments(commentStart: string, stringToParse: string) {
|
|
|
|
const diffFunc = (diffMe, diffBy) => diffMe.split(diffBy).join('');
|
|
|
|
let lines = stringToParse.split('\n');
|
|
|
|
lines = lines.map(line => {
|
|
|
|
return diffFunc(line, commentStart);
|
|
|
|
});
|
|
|
|
const cleanedString = lines.join('\n');
|
|
|
|
return this.parse(cleanedString);
|
|
|
|
}
|
2016-11-13 22:11:49 +00:00
|
|
|
}
|