6 Commits

Author SHA1 Message Date
6a1d91d5da 3.0.1 2022-06-26 20:45:40 +02:00
035e8dc0cd fix(core): update 2022-06-26 20:45:40 +02:00
57cadd079c 3.0.0 2022-05-28 21:05:37 +02:00
0ead80750a BREAKING CHANGE(core): switch to esm 2022-05-28 21:05:37 +02:00
3b2f8928de 2.0.14 2022-05-20 16:03:36 +02:00
92a1b589e7 fix(core): update 2022-05-20 16:03:35 +02:00
7 changed files with 1500 additions and 604 deletions

1993
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartmarkdown", "name": "@pushrocks/smartmarkdown",
"version": "2.0.13", "version": "3.0.1",
"private": false, "private": false,
"description": "do more with markdown files", "description": "do more with markdown files",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
@ -14,20 +14,24 @@
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.61", "@gitzone/tsbuild": "^2.1.61",
"@gitzone/tsrun": "^1.2.32", "@gitzone/tsrun": "^1.2.37",
"@gitzone/tstest": "^1.0.71", "@gitzone/tstest": "^1.0.71",
"@pushrocks/tapbundle": "^5.0.3", "@pushrocks/tapbundle": "^5.0.3",
"@types/node": "^17.0.34", "@types/node": "^18.0.0",
"tslint": "^6.1.3", "tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0" "tslint-config-prettier": "^1.18.0"
}, },
"dependencies": { "dependencies": {
"@pushrocks/smartyaml": "^2.0.5",
"@types/turndown": "^5.0.1", "@types/turndown": "^5.0.1",
"remark": "^14.0.2",
"remark-frontmatter": "^4.0.1", "remark-frontmatter": "^4.0.1",
"remark-gfm": "^3.0.1",
"remark-html": "^15.0.1", "remark-html": "^15.0.1",
"remark-parse": "^10.0.1",
"remark-stringify": "^10.0.2",
"turndown": "^7.1.1", "turndown": "^7.1.1",
"turndown-plugin-gfm": "^1.0.2" "turndown-plugin-gfm": "^1.0.2",
"unified": "^10.1.2"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",

View File

@ -10,10 +10,24 @@ tap.test('should create a valid instance of SmartMarkdown', async () => {
tap.test('should convert a markdown string to html', async () => { tap.test('should convert a markdown string to html', async () => {
const markdownString = '# Hi!'; const markdownString = '# Hi!';
const htmlString = await smartMarkdownInstance.markdownToHtml(markdownString); const mdParsedResult = await smartMarkdownInstance.getMdParsedResultFromMarkdown(markdownString);
const htmlString = mdParsedResult.html;
expect(htmlString).toEqual('<h1>Hi!</h1>\n'); expect(htmlString).toEqual('<h1>Hi!</h1>\n');
}); });
tap.test('should get frontmatter', async () => {
const markdownString = `---
hello: yes
---
# hello there
`;
const mdParsedResult = await smartMarkdownInstance.getMdParsedResultFromMarkdown(markdownString);
expect(mdParsedResult.frontmatterData.hello).toEqual('yes');
});
tap.test('should convert a html string to markdown', async () => { tap.test('should convert a html string to markdown', async () => {
const htmlString = '<h1 id="hi">Hi!</h1>\n<h2>This is it!</h2>'; const htmlString = '<h1 id="hi">Hi!</h1>\n<h2>This is it!</h2>';
const markdownString = smartMarkdownInstance.htmlToMarkdown(htmlString); const markdownString = smartMarkdownInstance.htmlToMarkdown(htmlString);

View File

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

View File

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

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 // pushrocks scope
import * as remark from 'remark'; import * as smartyaml from '@pushrocks/smartyaml';
import * as remarkFrontmatter from 'remark-frontmatter';
import * as remarkHtml from 'remark-html';
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 // other third party stuff
import turndown from 'turndown'; import turndown from 'turndown';