Compare commits
49 Commits
Author | SHA1 | Date | |
---|---|---|---|
4738e1e0e5 | |||
1d518b4f55 | |||
99718af936 | |||
7a02eeb0dd | |||
65ade5137b | |||
dfd39b406a | |||
2c9515fb86 | |||
cfa7807f8f | |||
ba3210c7f5 | |||
91424492ba | |||
d524a86921 | |||
6a1d91d5da | |||
035e8dc0cd | |||
57cadd079c | |||
0ead80750a | |||
3b2f8928de | |||
92a1b589e7 | |||
23f2cc9e6d | |||
813af0c232 | |||
16b21ff6bc | |||
c42f3ee1b7 | |||
dd1f8c928f | |||
75c62c01f5 | |||
6bd015e5bb | |||
97248d63db | |||
e6ef461347 | |||
fd52655e63 | |||
9a8dd46cdb | |||
d29d5dd07a | |||
8732b27445 | |||
2a5013fb56 | |||
64b50eeb1c | |||
7f1777e38b | |||
5654e6ed72 | |||
b3047fed9a | |||
ae580dd09e | |||
f470e591cc | |||
945b0ae8b8 | |||
ba90bb50a2 | |||
72de4bfb7d | |||
d195535ca0 | |||
989febe988 | |||
ebaa68a2fe | |||
effc3b10b1 | |||
1361e825fe | |||
a66e795432 | |||
1712f8e628 | |||
366856e904 | |||
173536be39 |
16
.gitignore
vendored
16
.gitignore
vendored
@ -1,6 +1,20 @@
|
||||
.nogit/
|
||||
node_modules/
|
||||
|
||||
# artifacts
|
||||
coverage/
|
||||
public/
|
||||
pages/
|
||||
|
||||
# installs
|
||||
node_modules/
|
||||
|
||||
# caches
|
||||
.yarn/
|
||||
.cache/
|
||||
.rpt2_cache
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_*/
|
||||
|
||||
# custom
|
109
.gitlab-ci.yml
109
.gitlab-ci.yml
@ -1,10 +1,10 @@
|
||||
# gitzone standard
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: "$CI_BUILD_STAGE"
|
||||
key: '$CI_BUILD_STAGE'
|
||||
|
||||
stages:
|
||||
- security
|
||||
@ -12,6 +12,9 @@ stages:
|
||||
- release
|
||||
- metadata
|
||||
|
||||
before_script:
|
||||
- npm install -g @shipzone/npmci
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
@ -19,50 +22,42 @@ mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
snyk:
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
- npmci command npm install --production --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=prod --production
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
allow_failure: true
|
||||
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=dev
|
||||
tags:
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
testLEGACY:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install legacy
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
allow_failure: true
|
||||
|
||||
testLTS:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
testSTABLE:
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
@ -72,7 +67,17 @@ testSTABLE:
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
release:
|
||||
stage: release
|
||||
@ -82,6 +87,7 @@ release:
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
@ -90,20 +96,15 @@ release:
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
image: docker:stable
|
||||
allow_failure: true
|
||||
services:
|
||||
- docker:stable-dind
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
||||
- docker run
|
||||
--env SOURCE_CODE="$PWD"
|
||||
--volume "$PWD":/code
|
||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
||||
artifacts:
|
||||
paths: [codeclimate.json]
|
||||
- npmci command npm install -g typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
@ -114,18 +115,20 @@ trigger:
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npm install -g typedoc typescript
|
||||
- npmci node install lts
|
||||
- npmci command npm install -g @git.zone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||
- npmci command tsdoc
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
@ -135,13 +138,3 @@ pages:
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
||||
|
||||
windowsCompatibility:
|
||||
image: stefanscherer/node-windows:10-build-tools
|
||||
stage: metadata
|
||||
script:
|
||||
- npm install & npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- windows
|
||||
allow_failure: true
|
||||
|
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"command": "npm test",
|
||||
"name": "Run npm test",
|
||||
"request": "launch",
|
||||
"type": "node-terminal"
|
||||
}
|
||||
]
|
||||
}
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"json.schemas": [
|
||||
{
|
||||
"fileMatch": ["/npmextra.json"],
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"npmci": {
|
||||
"type": "object",
|
||||
"description": "settings for npmci"
|
||||
},
|
||||
"gitzone": {
|
||||
"type": "object",
|
||||
"description": "settings for gitzone",
|
||||
"properties": {
|
||||
"projectType": {
|
||||
"type": "string",
|
||||
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
merge:
|
||||
- ../gitignore
|
||||
- ../ci_default
|
@ -1,6 +1,4 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Push.Rocks
|
||||
Copyright (c) 2014 Lossless GmbH (hello@lossless.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
@ -2,5 +2,33 @@
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "push.rocks",
|
||||
"gitrepo": "smartmarkdown",
|
||||
"description": "Enhances Markdown file handling with parsing, conversion, and frontmatter support.",
|
||||
"npmPackagename": "@push.rocks/smartmarkdown",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"markdown",
|
||||
"html conversion",
|
||||
"markdown parsing",
|
||||
"frontmatter",
|
||||
"remark",
|
||||
"unified",
|
||||
"turndown",
|
||||
"YAML",
|
||||
"plugin system",
|
||||
"text processing",
|
||||
"documentation",
|
||||
"content management"
|
||||
]
|
||||
}
|
||||
},
|
||||
"tsdoc": {
|
||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||
}
|
||||
}
|
13442
package-lock.json
generated
13442
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
70
package.json
70
package.json
@ -1,26 +1,68 @@
|
||||
{
|
||||
"name": "@pushrocks/smartmarkdown",
|
||||
"version": "1.0.4",
|
||||
"name": "@push.rocks/smartmarkdown",
|
||||
"version": "3.0.3",
|
||||
"private": false,
|
||||
"description": "do more with markdown files",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"description": "Enhances Markdown file handling with parsing, conversion, and frontmatter support.",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "tstest test/",
|
||||
"format": "(gitzone format)",
|
||||
"build": "tsbuild"
|
||||
"build": "tsbuild --web --allowimplicitany"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tsrun": "^1.1.12",
|
||||
"@gitzone/tstest": "^1.0.15",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"@types/node": "^10.10.3"
|
||||
"@git.zone/tsbuild": "^2.1.61",
|
||||
"@git.zone/tsrun": "^1.2.37",
|
||||
"@git.zone/tstest": "^1.0.86",
|
||||
"@push.rocks/tapbundle": "^5.0.3",
|
||||
"@types/node": "^20.11.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/marked": "^0.4.1",
|
||||
"marked": "^0.5.0"
|
||||
"@push.rocks/smartyaml": "^2.0.5",
|
||||
"@types/turndown": "^5.0.1",
|
||||
"remark-frontmatter": "^5.0.0",
|
||||
"remark-gfm": "^4.0.0",
|
||||
"remark-html": "^16.0.1",
|
||||
"remark-parse": "^11.0.0",
|
||||
"remark-stringify": "^11.0.0",
|
||||
"turndown": "^7.1.1",
|
||||
"turndown-plugin-gfm": "^1.0.2",
|
||||
"unified": "^11.0.4"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_*/**/*",
|
||||
"dist_ts/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
],
|
||||
"keywords": [
|
||||
"markdown",
|
||||
"html conversion",
|
||||
"markdown parsing",
|
||||
"frontmatter",
|
||||
"remark",
|
||||
"unified",
|
||||
"turndown",
|
||||
"YAML",
|
||||
"plugin system",
|
||||
"text processing",
|
||||
"documentation",
|
||||
"content management"
|
||||
],
|
||||
"homepage": "https://code.foss.global/push.rocks/smartmarkdown",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://code.foss.global/push.rocks/smartmarkdown.git"
|
||||
}
|
||||
}
|
6758
pnpm-lock.yaml
generated
Normal file
6758
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
1
readme.hints.md
Normal file
1
readme.hints.md
Normal file
@ -0,0 +1 @@
|
||||
|
129
readme.md
Normal file
129
readme.md
Normal file
@ -0,0 +1,129 @@
|
||||
# @push.rocks/smartmarkdown
|
||||
do more with markdown files
|
||||
|
||||
## Install
|
||||
To install `@push.rocks/smartmarkdown`, you can use npm or yarn. Run one of the following commands in your terminal:
|
||||
|
||||
```bash
|
||||
npm install @push.rocks/smartmarkdown --save
|
||||
```
|
||||
|
||||
or if you use yarn:
|
||||
|
||||
```bash
|
||||
yarn add @push.rocks/smartmarkdown
|
||||
```
|
||||
|
||||
This module is designed to be used in a Node.js environment or in a frontend project that supports ES Modules.
|
||||
|
||||
## Usage
|
||||
`@push.rocks/smartmarkdown` offers powerful tools to work with markdown files, including parsing markdown to HTML, extracting frontmatter data, and converting HTML back to markdown. Below, we'll explore how to utilize these features effectively.
|
||||
|
||||
### **Parsing Markdown to HTML**
|
||||
|
||||
Let's start by converting a simple Markdown string to HTML:
|
||||
|
||||
```typescript
|
||||
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
|
||||
|
||||
async function parseMarkdown() {
|
||||
const mdString = `# Hello World
|
||||
|
||||
This is a simple markdown string.`;
|
||||
const htmlString = await SmartMarkdown.easyMarkdownToHtml(mdString);
|
||||
console.log(htmlString); // Logs the HTML string
|
||||
}
|
||||
|
||||
parseMarkdown();
|
||||
```
|
||||
|
||||
In this example, `SmartMarkdown.easyMarkdownToHtml` is a convenient static method that takes a markdown string and returns its HTML representation.
|
||||
|
||||
### **Working with Frontmatter**
|
||||
|
||||
Markdown files often contain frontmatter, which is metadata specified at the top of the file. `@push.rocks/smartmarkdown` can extract this data along with converting the content.
|
||||
|
||||
```typescript
|
||||
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
|
||||
|
||||
async function parseMarkdownWithFrontmatter() {
|
||||
const markdownWithFrontmatter = `---
|
||||
title: "My First Post"
|
||||
date: 2023-01-01
|
||||
tags: ["blog", "post"]
|
||||
---
|
||||
|
||||
# Hello World
|
||||
|
||||
This is a post with frontmatter.`;
|
||||
|
||||
const smartmarkdownInstance = new SmartMarkdown();
|
||||
const mdParsedResult = await smartmarkdownInstance.getMdParsedResultFromMarkdown(markdownWithFrontmatter);
|
||||
console.log(mdParsedResult.frontmatterData); // Logs: { title: "My First Post", date: "2023-01-01", tags: ["blog", "post"] }
|
||||
console.log(mdParsedResult.html); // Logs: HTML content
|
||||
}
|
||||
|
||||
parseMarkdownWithFrontmatter();
|
||||
```
|
||||
|
||||
In the code above, we manually create an instance of `SmartMarkdown` to access the `getMdParsedResultFromMarkdown` method, which returns an object containing both the HTML and the frontmatter data.
|
||||
|
||||
### **HTML to Markdown Conversion**
|
||||
|
||||
Sometimes, you may need to convert HTML back to Markdown. Here's how you can do it:
|
||||
|
||||
```typescript
|
||||
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
|
||||
|
||||
const smartmarkdownInstance = new SmartMarkdown();
|
||||
const htmlString = '<h1>Hello World</h1><p>This is a simple HTML string.</p>';
|
||||
const markdownString = smartmarkdownInstance.htmlToMarkdown(htmlString);
|
||||
console.log(markdownString);
|
||||
```
|
||||
|
||||
In this example, we use the `htmlToMarkdown` method to convert an HTML string back to Markdown. This is particularly useful when working with content editing interfaces that support both formats.
|
||||
|
||||
### **Advanced Usage: Parsing Markdown with Custom Plugins**
|
||||
|
||||
`@push.rocks/smartmarkdown` leverages Unified, Remark, and Turndown to provide its functionality. You can extend its capabilities by using custom plugins.
|
||||
|
||||
For instance, if you wanted to use a custom Remark plugin to highlight code blocks, you would:
|
||||
|
||||
1. Create a new SmartMarkdown instance.
|
||||
2. Configure it to use your custom plugins.
|
||||
3. Process your markdown content.
|
||||
|
||||
```typescript
|
||||
// This is a hypothetical example. Please refer to the respective plugin documentation for actual implementation details.
|
||||
|
||||
import { SmartMarkdown } from '@push.rocks/smartmarkdown';
|
||||
import myRemarkPlugin from 'remark-myplugin';
|
||||
|
||||
async function parseMarkdownWithPlugin(mdString: string) {
|
||||
const smartMarkdownInstance = new SmartMarkdown();
|
||||
smartMarkdownInstance.useRemarkPlugin(myRemarkPlugin, pluginOptions);
|
||||
const result = await smartMarkdownInstance.getMdParsedResultFromMarkdown(mdString);
|
||||
// Now `result` will include transformations done by your custom plugin.
|
||||
}
|
||||
```
|
||||
|
||||
`@push.rocks/smartmarkdown` provides a solid base for performing various markdown related tasks in your projects. Whether parsing, transforming, or exporting content, its utilities and integrations offer flexibility for most needs. By leveraging ES Modules and TypeScript, it ensures type safety and enhances developer experience with excellent IntelliSense support.
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||
|
||||
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||
|
||||
### Trademarks
|
||||
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||
|
||||
### Company Information
|
||||
|
||||
Task Venture Capital GmbH
|
||||
Registered at District court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
|
||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
37
test/test.both.ts
Normal file
37
test/test.both.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as smartmarkdown from '../ts/index.js';
|
||||
|
||||
let smartMarkdownInstance: smartmarkdown.SmartMarkdown;
|
||||
|
||||
tap.test('should create a valid instance of SmartMarkdown', async () => {
|
||||
smartMarkdownInstance = new smartmarkdown.SmartMarkdown();
|
||||
expect(smartMarkdownInstance).toBeInstanceOf(smartmarkdown.SmartMarkdown);
|
||||
});
|
||||
|
||||
tap.test('should convert a markdown string to html', async () => {
|
||||
const markdownString = '# Hi!';
|
||||
const mdParsedResult = await smartMarkdownInstance.getMdParsedResultFromMarkdown(markdownString);
|
||||
const htmlString = mdParsedResult.html;
|
||||
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 () => {
|
||||
const htmlString = '<h1 id="hi">Hi!</h1>\n<h2>This is it!</h2>';
|
||||
const markdownString = smartMarkdownInstance.htmlToMarkdown(htmlString);
|
||||
console.log(markdownString);
|
||||
});
|
||||
|
||||
tap.start();
|
@ -1,9 +0,0 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartmarkdown from '../ts/index';
|
||||
|
||||
tap.test('first test', async () => {
|
||||
const mdString = smartmarkdown.markdownToHtml('# Hi!');
|
||||
expect(mdString).to.equal('<h1 id="hi-">Hi!</h1>\n');
|
||||
});
|
||||
|
||||
tap.start();
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartmarkdown',
|
||||
version: '3.0.3',
|
||||
description: 'do more with markdown files'
|
||||
}
|
31
ts/index.ts
31
ts/index.ts
@ -1,3 +1,30 @@
|
||||
import * as plugins from './smartmarkdown.plugins';
|
||||
import * as plugins from './smartmarkdown.plugins.js';
|
||||
import { MdParsedResult } from './smartmarkdown.classes.mdparsedresult.js';
|
||||
|
||||
export const markdownToHtml = (mdString: string): string => plugins.marked(mdString);
|
||||
export class SmartMarkdown {
|
||||
public static async easyMarkdownToHtml(mdStringArg: string) {
|
||||
const smartmarkdownInstance = new SmartMarkdown();
|
||||
const mdParsedResult = await smartmarkdownInstance.getMdParsedResultFromMarkdown(mdStringArg);
|
||||
return mdParsedResult.html;
|
||||
}
|
||||
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* create a MdParsedResult from markdown
|
||||
* @param mdStringArg
|
||||
*/
|
||||
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({
|
||||
headingStyle: 'atx',
|
||||
codeBlockStyle: 'fenced',
|
||||
});
|
||||
turndownInstance.use(plugins.turndownPluginGfm.gfm);
|
||||
return turndownInstance.turndown(htmlString);
|
||||
}
|
||||
}
|
||||
|
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,3 +1,23 @@
|
||||
import marked = require('marked');
|
||||
// pushrocks scope
|
||||
import * as smartyaml from '@push.rocks/smartyaml';
|
||||
|
||||
export { marked };
|
||||
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';
|
||||
// @ts-ignore
|
||||
import * as turndownPluginGfm from 'turndown-plugin-gfm';
|
||||
|
||||
export { turndown, turndownPluginGfm };
|
||||
|
14
tsconfig.json
Normal file
14
tsconfig.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true
|
||||
},
|
||||
"exclude": [
|
||||
"dist_*/**/*.d.ts"
|
||||
]
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
{
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"]
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user