Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
6a1d91d5da | |||
035e8dc0cd | |||
57cadd079c | |||
0ead80750a | |||
3b2f8928de | |||
92a1b589e7 |
1993
package-lock.json
generated
1993
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
14
package.json
14
package.json
@ -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/**/*",
|
||||||
|
@ -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);
|
||||||
|
@ -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'
|
||||||
}
|
}
|
||||||
|
21
ts/index.ts
21
ts/index.ts
@ -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 {
|
||||||
|
38
ts/smartmarkdown.classes.mdparsedresult.ts
Normal file
38
ts/smartmarkdown.classes.mdparsedresult.ts
Normal 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 = {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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';
|
||||||
|
Reference in New Issue
Block a user