9 Commits

Author SHA1 Message Date
90f8c11288 0.1.5 2023-05-06 14:07:18 -07:00
4ceff93214 updates to frontmatter handling 2023-05-06 14:07:15 -07:00
a2866cade6 0.1.4 2023-05-06 14:05:00 -07:00
48041b5845 fix main declaration 2023-05-06 14:04:56 -07:00
7bbebb4574 0.1.3 2023-05-06 14:03:45 -07:00
5c05a87981 add npmignore 2023-05-06 14:03:40 -07:00
60826941fa 0.1.2 2023-05-06 14:01:23 -07:00
29aab733b0 fix main declaration 2023-05-06 14:01:11 -07:00
7bb0e3a072 readme 2023-04-30 19:01:33 -07:00
5 changed files with 33 additions and 19 deletions

2
.npmignore Normal file
View File

@@ -0,0 +1,2 @@
src
node_modules

4
package-lock.json generated
View File

@@ -29,7 +29,7 @@
"@types/qrcode": "^1.5.0",
"typescript": "^5.0.4"
},
"version": "0.1.1"
"version": "0.1.5"
},
"node_modules/@tootallnate/once": {
"version": "2.0.0",
@@ -3483,5 +3483,5 @@
}
}
},
"version": "0.1.1"
"version": "0.1.5"
}

View File

@@ -1,16 +1,17 @@
{
"name": "@doc-utils/markdown2html",
"version": "0.1.1",
"version": "0.1.5",
"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",

View File

@@ -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
@@ -67,7 +70,7 @@ foo:
const { frontmatter, markdown } = 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"
```
@@ -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

View File

@@ -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;
if (end_index === 2) {
return {
frontmatter: null,
document: raw_document,
};
}
return { frontmatter, markdown };
const formatter_yaml = raw_document.slice(3, end_index);
const document = raw_document.slice(end_index + 4);
const frontmatter = parse ? formatter_yaml : yaml.parse(formatter_yaml);
return { frontmatter, document };
}