fix(core): update

This commit is contained in:
2022-05-20 16:03:35 +02:00
parent 23f2cc9e6d
commit 92a1b589e7
7 changed files with 542 additions and 63 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@pushrocks/smartmarkdown',
version: '2.0.13',
version: '2.0.14',
description: 'do more with markdown files'
}

View File

@ -1,20 +1,17 @@
import * as plugins from './smartmarkdown.plugins.js';
import { MdParsedResult } from './smartmarkdown.classes.mdparsedresult.js';
export class SmartMarkdown {
constructor() {}
/**
* converts markdown to html
* @param mdString
* create a MdParsedResult from markdown
* @param mdStringArg
*/
public async markdownToHtml(mdString: string): Promise<string> {
const result = await plugins
.remark.remark()
.use(plugins.remarkHtml.default)
.use(plugins.remarkFrontmatter.default, ['yaml', 'toml'])
.process(mdString);
return result.toString();
}
public async getMdParsedResultFromMarkdown(mdStringArg: string): Promise<MdParsedResult> {
const result = await MdParsedResult.createFromMarkdownString(mdStringArg);
return result;
};
public htmlToMarkdown(htmlString: string): string {
const turndownInstance = new plugins.turndown({

View File

@ -0,0 +1,38 @@
import * as plugins from './smartmarkdown.plugins.js';
export class MdParsedResult {
public static async createFromMarkdownString(mdStringArg: string): Promise<MdParsedResult> {
const mdParsedResult = new MdParsedResult();
await mdParsedResult.updateFromMarkdownString(mdStringArg);
return mdParsedResult;
}
public originalString: string;
public title: string;
public html: string;
public frontmatterData: {[key: string]: any};
public async updateFromMarkdownString(mdStringArg: string) {
let yamlString: string;
const result = await plugins.unified()
.use(plugins.remarkParse)
.use(plugins.remarkGfm)
.use(plugins.remarkFrontmatter, ['yaml', 'toml'])
.use(plugins.remarkStringify)
.use(plugins.remarkHtml)
.use(() => (tree) => {
console.dir(tree);
const yamlChild = tree.children.find(objectArg => objectArg.type === 'yaml');
if (yamlChild) {
yamlString = (yamlChild as any).value;
}
})
.process(mdStringArg);
this.html = result.toString();
if (yamlString) {
this.frontmatterData = await plugins.smartyaml.yamlStringToObject(yamlString);
} else {
this.frontmatterData = {};
}
}
}

View File

@ -1,9 +1,19 @@
// third party remark
import * as remark from 'remark';
import * as remarkFrontmatter from 'remark-frontmatter';
import * as remarkHtml from 'remark-html';
// pushrocks scope
import * as smartyaml from '@pushrocks/smartyaml';
export { remark, remarkFrontmatter, remarkHtml };
export {
smartyaml
}
// third party remark
import { unified } from 'unified';
import remarkGfm from 'remark-gfm';
import remarkParse from 'remark-parse';
import remarkFrontmatter from 'remark-frontmatter';
import remarkHtml from 'remark-html';
import remarkStringify from 'remark-stringify';
export { unified, remarkGfm, remarkParse, remarkFrontmatter, remarkHtml, remarkStringify };
// other third party stuff
import turndown from 'turndown';