Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
3553ced719
|
|||
715451f02f
|
|||
b2de8df538
|
|||
25ae8b0bcb
|
|||
b5a427d669
|
|||
455915dd98
|
|||
90f8c11288
|
|||
4ceff93214
|
|||
a2866cade6
|
|||
48041b5845
|
|||
7bbebb4574
|
|||
5c05a87981
|
|||
60826941fa
|
|||
29aab733b0
|
|||
7bb0e3a072
|
2
.npmignore
Normal file
2
.npmignore
Normal file
@@ -0,0 +1,2 @@
|
||||
src
|
||||
node_modules
|
39
package-lock.json
generated
39
package-lock.json
generated
@@ -1,10 +1,12 @@
|
||||
{
|
||||
"name": "markdown-to-html",
|
||||
"name": "@doc-utils/markdown2html",
|
||||
"version": "0.1.8",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "markdown-to-html",
|
||||
"name": "@doc-utils/markdown2html",
|
||||
"version": "0.1.8",
|
||||
"dependencies": {
|
||||
"bytefield-svg": "^1.6.1",
|
||||
"dompurify": "^2.3.6",
|
||||
@@ -18,6 +20,9 @@
|
||||
"vega": "^5.22.1",
|
||||
"yaml": "^2.2.2"
|
||||
},
|
||||
"bin": {
|
||||
"markdown2html": "bin/markdown2html"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/dompurify": "^2.3.3",
|
||||
"@types/jsdom": "^20.0.0",
|
||||
@@ -28,8 +33,7 @@
|
||||
"@types/prismjs": "^1.26.0",
|
||||
"@types/qrcode": "^1.5.0",
|
||||
"typescript": "^5.0.4"
|
||||
},
|
||||
"version": "0.1.1"
|
||||
}
|
||||
},
|
||||
"node_modules/@tootallnate/once": {
|
||||
"version": "2.0.0",
|
||||
@@ -82,15 +86,15 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/marked": {
|
||||
"version": "4.0.8",
|
||||
"resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.0.8.tgz",
|
||||
"integrity": "sha512-HVNzMT5QlWCOdeuBsgXP8EZzKUf0+AXzN+sLmjvaB3ZlLqO+e4u0uXrdw9ub69wBKFs+c6/pA4r9sy6cCDvImw==",
|
||||
"version": "4.3.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.3.0.tgz",
|
||||
"integrity": "sha512-zK4gSFMjgslsv5Lyvr3O1yCjgmnE4pr8jbG8qVn4QglMwtpvPCf4YT2Wma7Nk95OxUUJI8Z+kzdXohbM7mVpGw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "18.16.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.3.tgz",
|
||||
"integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==",
|
||||
"version": "18.16.5",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.5.tgz",
|
||||
"integrity": "sha512-seOA34WMo9KB+UA78qaJoCO20RJzZGVXQ5Sh6FWu0g/hfT44nKXnej3/tCQl7FL97idFpBhisLYCTB50S0EirA==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/prismjs": {
|
||||
@@ -2011,15 +2015,15 @@
|
||||
"dev": true
|
||||
},
|
||||
"@types/marked": {
|
||||
"version": "4.0.8",
|
||||
"resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.0.8.tgz",
|
||||
"integrity": "sha512-HVNzMT5QlWCOdeuBsgXP8EZzKUf0+AXzN+sLmjvaB3ZlLqO+e4u0uXrdw9ub69wBKFs+c6/pA4r9sy6cCDvImw==",
|
||||
"version": "4.3.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/marked/-/marked-4.3.0.tgz",
|
||||
"integrity": "sha512-zK4gSFMjgslsv5Lyvr3O1yCjgmnE4pr8jbG8qVn4QglMwtpvPCf4YT2Wma7Nk95OxUUJI8Z+kzdXohbM7mVpGw==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/node": {
|
||||
"version": "18.16.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.3.tgz",
|
||||
"integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==",
|
||||
"version": "18.16.5",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.5.tgz",
|
||||
"integrity": "sha512-seOA34WMo9KB+UA78qaJoCO20RJzZGVXQ5Sh6FWu0g/hfT44nKXnej3/tCQl7FL97idFpBhisLYCTB50S0EirA==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/prismjs": {
|
||||
@@ -3482,6 +3486,5 @@
|
||||
"decamelize": "^1.2.0"
|
||||
}
|
||||
}
|
||||
},
|
||||
"version": "0.1.1"
|
||||
}
|
||||
}
|
||||
|
@@ -1,16 +1,17 @@
|
||||
{
|
||||
"name": "@doc-utils/markdown2html",
|
||||
"version": "0.1.1",
|
||||
"version": "0.1.8",
|
||||
"publishConfig": {
|
||||
"registry": "https://gitea.home.jbrumond.me/api/packages/doc-utils/npm/"
|
||||
},
|
||||
"scripts": {
|
||||
"tsc": "tsc"
|
||||
"tsc": "tsc",
|
||||
"clean": "rm -rf ./build"
|
||||
},
|
||||
"bin": {
|
||||
"markdown2html": "./bin/markdown2html"
|
||||
},
|
||||
"exports": "./build/index.js",
|
||||
"main": "./build/index.js",
|
||||
"devDependencies": {
|
||||
"@types/dompurify": "^2.3.3",
|
||||
"@types/jsdom": "^20.0.0",
|
||||
|
19
readme.md
19
readme.md
@@ -22,8 +22,11 @@ These are the primary projects used to render various types of content:
|
||||
# Update project npm config to refer to correct registry for the @doc-utils scope
|
||||
echo '@doc-utils:registry=https://gitea.jbrumond.me/api/packages/doc-utils/npm/' >> ./.npmrc
|
||||
|
||||
# Install package
|
||||
# Install package for programatic use
|
||||
npm install --save @doc-utils/markdown2html
|
||||
|
||||
# Install globally for CLI usage
|
||||
npm install --global @doc-utils/markdown2html
|
||||
```
|
||||
|
||||
|
||||
@@ -50,13 +53,13 @@ async function main() {
|
||||
}
|
||||
```
|
||||
|
||||
### Handling Frontmatter
|
||||
### Handling front matter
|
||||
|
||||
```ts
|
||||
import { process_frontmatter } from '@doc-utils/markdown2html';
|
||||
|
||||
const raw_content = `---
|
||||
title: Example Markdown with Frontmatter
|
||||
title: Example Markdown with front matter
|
||||
foo:
|
||||
- bar
|
||||
- baz
|
||||
@@ -65,11 +68,11 @@ foo:
|
||||
# This is some markdown
|
||||
`;
|
||||
|
||||
const { frontmatter, markdown } = process_frontmatter(raw_content);
|
||||
const { frontmatter, document } = process_frontmatter(raw_content);
|
||||
|
||||
console.log(frontmatter.title); // "Example Markdown with Frontmatter"
|
||||
console.log(frontmatter.title); // "Example Markdown with front matter"
|
||||
console.log(frontmatter.foo); // [ "bar", "baz" ]
|
||||
console.log(markdown); // "\n# This is some markdown\n"
|
||||
console.log(document); // "\n# This is some markdown\n"
|
||||
```
|
||||
|
||||
|
||||
@@ -80,11 +83,11 @@ console.log(markdown); // "\n# This is some markdown\n"
|
||||
echo '# This is some markdown' | ./bin/markdown2html --base-url 'https://example.com' > output.html
|
||||
```
|
||||
|
||||
### Handling Frontmatter
|
||||
### Handling front matter
|
||||
|
||||
```bash
|
||||
filecontents="---
|
||||
title: Example Markdown with Frontmatter
|
||||
title: Example Markdown with front matter
|
||||
foo:
|
||||
- bar
|
||||
- baz
|
||||
|
@@ -88,7 +88,5 @@ export function parse_attributes(text: string, fallback_id?: Lazy<string>) {
|
||||
}
|
||||
}
|
||||
|
||||
console.log(attrs);
|
||||
|
||||
return attrs;
|
||||
}
|
||||
|
@@ -1,18 +1,26 @@
|
||||
|
||||
import * as yaml from 'yaml';
|
||||
|
||||
export function process_frontmatter(document: string, parse = true) {
|
||||
if (! document.startsWith('---\n')) {
|
||||
export function process_frontmatter(raw_document: string, parse = true) {
|
||||
if (! raw_document.startsWith('---\n')) {
|
||||
return {
|
||||
frontmatter: null,
|
||||
markdown: document,
|
||||
document: raw_document,
|
||||
};
|
||||
}
|
||||
|
||||
const endIndex = document.slice(3).indexOf('\n---\n') + 3;
|
||||
const frontmatterYaml = document.slice(3, endIndex);
|
||||
const markdown = document.slice(endIndex + 4);
|
||||
const frontmatter = parse ? frontmatterYaml : yaml.parse(frontmatterYaml);
|
||||
const end_index = raw_document.slice(3).indexOf('\n---\n') + 3;
|
||||
|
||||
return { frontmatter, markdown };
|
||||
if (end_index === 2) {
|
||||
return {
|
||||
frontmatter: null,
|
||||
document: raw_document,
|
||||
};
|
||||
}
|
||||
|
||||
const formatter_yaml = raw_document.slice(3, end_index);
|
||||
const document = raw_document.slice(end_index + 4);
|
||||
const frontmatter = parse ? yaml.parse(formatter_yaml) : formatter_yaml;
|
||||
|
||||
return { frontmatter, document };
|
||||
}
|
||||
|
Reference in New Issue
Block a user