Skip to content

Commit 925ba0d

Browse files
committed
Support simple yaml metadata
1 parent c48f6e6 commit 925ba0d

File tree

5 files changed

+58
-24
lines changed

5 files changed

+58
-24
lines changed

lib/container.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// modified from https://github.com/markdown-it/markdown-it-container
22

33
const names = ['success', 'info', 'warning', 'danger', 'spoiler']
4-
export function MarkdownItContainer (md: any, _options: any) {
4+
export function MarkdownItContainer(md: any, _options: any) {
55
// Second param may be useful if you decide
66
// to increase minimal allowed marker length
77

8-
function renderContainer (tokens: any, idx: number, _options: any, env: any, slf: any): string {
8+
function renderContainer(tokens: any, idx: number, _options: any, env: any, slf: any): string {
99
// add a class to the opening tag
1010
if (tokens[idx].nesting === 1) {
1111
tokens[idx].attrJoin('class', tokens[idx].name)
@@ -14,7 +14,7 @@ export function MarkdownItContainer (md: any, _options: any) {
1414
return slf.renderToken(tokens, idx, _options, env, slf)
1515
}
1616

17-
function renderSpoiler (tokens: any, idx: number, _options: any, env: any, slf: any): string {
17+
function renderSpoiler(tokens: any, idx: number, _options: any, env: any, slf: any): string {
1818
// add a class to the opening tag
1919
if (tokens[idx].nesting === 1) {
2020
let summary: string = tokens[idx].summary
@@ -34,7 +34,7 @@ export function MarkdownItContainer (md: any, _options: any) {
3434
const markerChar = markerStr.charCodeAt(0)
3535
const markerLen = markerStr.length
3636

37-
function rule (state: any, startLine: number, endLine: number, silent: any): boolean {
37+
function rule(state: any, startLine: number, endLine: number, silent: any): boolean {
3838
let pos, nextLine, token
3939

4040
let autoClosed = false

lib/converter.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ const MarkdownItTOC = require('markdown-it-table-of-contents')
1919
const MarkdownItAnchor = require('markdown-it-anchor')
2020
const MarkdownItRuby = require('markdown-it-ruby')
2121
const MarkdownItCheckbox = require('markdown-it-checkbox')
22+
const htmlEncode = require('htmlencode').htmlEncode;
2223

2324
export class Convert {
2425
src: Array<string>
2526
dest: string
2627
layout: string
2728
md: any
29+
title: string
2830

29-
constructor (src: Array<string>, dest: string, layout: string, hardBreak: boolean) {
31+
constructor(src: Array<string>, dest: string, layout: string, hardBreak: boolean) {
3032
this.src = src
3133
this.dest = dest
3234
this.layout = layout
35+
this.title = ''
3336

3437
// https://hackmd.io/c/codimd-documentation/%2F%40codimd%2Fmarkdown-syntax
3538
this.md = new MarkdownIt({
@@ -39,7 +42,11 @@ export class Convert {
3942
typographer: true
4043
})
4144
.use(MarkdownItMathJax())
42-
.use(MarkdownItYAMLMetadata)
45+
.use(MarkdownItYAMLMetadata, (option: any) => {
46+
if ('title' in option) {
47+
this.title = option.title as string
48+
}
49+
})
4350
.use(MarkdownItSub)
4451
.use(MarkdownItSup)
4552
.use(MarkdownItFootnote)
@@ -63,10 +70,10 @@ export class Convert {
6370

6471
// @param html: html string
6572
// @return: html string with layout
66-
private addLayout (html: string): string {
73+
private addLayout(title: string, html: string): string {
6774
if (fs.existsSync(this.layout)) {
6875
const layout = fs.readFileSync(this.layout, { encoding: 'utf-8' })
69-
return layout.replace('{{main}}', html)
76+
return layout.replace('{{title}}', htmlEncode(title)).replace('{{main}}', html)
7077
}
7178

7279
console.error(`${this.layout} is not found`)
@@ -75,19 +82,15 @@ export class Convert {
7582

7683
// @param filepath: the path of the file should be converted
7784
// this function doesn't check the ext name of filepath
78-
public convertFile (filepath: string) {
85+
public convertFile(filepath: string) {
7986
const markdown = fs.readFileSync(filepath, { encoding: 'utf-8' })
80-
// process yaml metadata
81-
// if (markdown.slice(0, 3) === '---') {
82-
// markdown = '<!--yaml-metadata-->' + markdown
83-
// }
8487
const html = this.md.render(markdown)
85-
const res = this.addLayout(html)
88+
const res = this.addLayout(this.title, html)
8689
const basename = path.basename(filepath)
8790
fs.writeFileSync(path.join(this.dest, basename.replace(/\.md$/, '.html')), res)
8891
}
8992

90-
public convertBatch () {
93+
public convertBatch() {
9194
if (!fs.existsSync(this.dest)) {
9295
fs.mkdirSync(this.dest)
9396
}

lib/yaml-metadata.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
import YAML from 'yaml'
2+
13
// modified from https://github.com/flaviotordini/markdown-it-yaml
24
const tokenType = 'yaml_metadata'
35

4-
export function MarkdownItYAMLMetadata (md: any, _options: any) {
5-
function getLine (state: any, line: any): string {
6+
export function MarkdownItYAMLMetadata(md: any, callback: undefined | ((option: any) => any)) {
7+
function getLine(state: any, line: any): string {
68
const pos = state.bMarks[line]
79
const max = state.eMarks[line]
810
return state.src.substring(pos, max)
911
}
1012

11-
function rule (state: any, startLine: number, endLine: number, silent: any): boolean {
13+
function rule(state: any, startLine: number, endLine: number, silent: any): boolean {
1214
if (state.blkIndent !== 0 || state.tShift[startLine] < 0) {
1315
return false
1416
}
@@ -38,18 +40,20 @@ export function MarkdownItYAMLMetadata (md: any, _options: any) {
3840

3941
const dataStartPos = state.bMarks[dataStart]
4042
const dataEndPos = state.eMarks[dataEnd]
41-
const yaml = state.src.substring(dataStartPos, dataEndPos)
4243

4344
const token = state.push(tokenType, '', 0)
44-
token.content = yaml
45+
token.yaml = state.src.substring(dataStartPos, dataEndPos)
4546

4647
state.line = nextLine + 1
4748
return true
4849
}
4950

50-
function renderer (tokens: any, idx: number, _options: any, _evn: any): string {
51+
function renderer(tokens: any, idx: number, _options: any, _evn: any): string {
5152
const token = tokens[idx]
52-
return `<!--yaml\n${token.content}\n-->`
53+
if (callback) {
54+
callback(YAML.parse(token.yaml))
55+
}
56+
return `<!--yaml\n${token.yaml}\n-->`
5357
}
5458

5559
md.block.ruler.before('hr', tokenType, rule)

package-lock.json

Lines changed: 26 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@gerhobbelt/markdown-it-checkbox": "^1.2.0-3",
3030
"@types/node": "^16.11.11",
3131
"commander": "^8.3.0",
32+
"htmlencode": "^0.0.4",
3233
"markdown-it": "^12.2.0",
3334
"markdown-it-abbr": "^1.0.4",
3435
"markdown-it-anchor": "^8.4.1",
@@ -45,7 +46,8 @@
4546
"markdown-it-sup": "^1.0.0",
4647
"markdown-it-table-of-contents": "^0.6.0",
4748
"markdown-it-yaml": "^1.0.0",
48-
"typescript": "^4.5.2"
49+
"typescript": "^4.5.2",
50+
"yaml": "^2.2.1"
4951
},
5052
"devDependencies": {
5153
"@typescript-eslint/eslint-plugin": "^5.50.0",

0 commit comments

Comments
 (0)