@sullux/markdown-html 1.0.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -60,17 +60,19 @@ const processBlockNode = (node, style, blocks, currentInline, traverse, toAst) =
|
|
|
60
60
|
|
|
61
61
|
if (tagName === 'ul' || tagName === 'ol') {
|
|
62
62
|
flushInline()
|
|
63
|
-
const
|
|
63
|
+
const children = []
|
|
64
64
|
for (const child of node.children) {
|
|
65
65
|
if (child.tagName === 'li') {
|
|
66
66
|
const itemInline = []
|
|
67
67
|
for (const liChild of child.children) traverse(liChild, style, itemInline)
|
|
68
|
-
if (itemInline.length > 0)
|
|
68
|
+
if (itemInline.length > 0) {
|
|
69
|
+
children.push({ type: 'listItem', children: [{ type: 'paragraph', children: itemInline }] })
|
|
70
|
+
}
|
|
69
71
|
} else {
|
|
70
72
|
traverse(child, style, currentInline)
|
|
71
73
|
}
|
|
72
74
|
}
|
|
73
|
-
if (
|
|
75
|
+
if (children.length > 0) blocks.push({ type: tagName === 'ul' ? 'bulletList' : 'orderedList', tight: true, children })
|
|
74
76
|
return true
|
|
75
77
|
}
|
|
76
78
|
|
|
@@ -3,7 +3,7 @@ const { renderBlock } = require('./render-block')
|
|
|
3
3
|
|
|
4
4
|
const markdownToHtml = (markdown, options = {}) => {
|
|
5
5
|
if (!markdown) return ''
|
|
6
|
-
const ast = parse(markdown)
|
|
6
|
+
const ast = typeof markdown === 'string' ? parse(markdown) : markdown
|
|
7
7
|
const ctx = { ...options, usedSlugs: new Set() }
|
|
8
8
|
return ast.blocks.map((block) => renderBlock(block, ctx)).join('')
|
|
9
9
|
}
|
|
@@ -2,6 +2,7 @@ const { escapeHtml } = require('./escape')
|
|
|
2
2
|
const { slugify } = require('./slugify')
|
|
3
3
|
const { renderInline } = require('./render-inline')
|
|
4
4
|
const { highlightCode } = require('./highlight')
|
|
5
|
+
const { renderList } = require('./render-list')
|
|
5
6
|
|
|
6
7
|
const renderBlock = (node, options = {}) => {
|
|
7
8
|
if (!node) return ''
|
|
@@ -35,13 +36,9 @@ const renderBlock = (node, options = {}) => {
|
|
|
35
36
|
const innerHtml = node.children ? node.children.map((child) => renderBlock(child, options)).join('') : ''
|
|
36
37
|
return `<div class="callout callout-${style}">\n<div class="callout-title">${title}</div>\n${innerHtml}</div>\n`
|
|
37
38
|
}
|
|
38
|
-
case 'bulletList':
|
|
39
|
-
const itemsHtml = node.items ? node.items.map((item) => `<li>${renderInline(item, options)}</li>\n`).join('') : ''
|
|
40
|
-
return `<ul>\n${itemsHtml}</ul>\n`
|
|
41
|
-
}
|
|
39
|
+
case 'bulletList':
|
|
42
40
|
case 'orderedList': {
|
|
43
|
-
|
|
44
|
-
return `<ol>\n${itemsHtml}</ol>\n`
|
|
41
|
+
return renderList(node, options, renderBlock)
|
|
45
42
|
}
|
|
46
43
|
case 'table': {
|
|
47
44
|
const alignments = node.alignments || []
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const { renderInline } = require('./render-inline')
|
|
2
|
+
|
|
3
|
+
const renderListItem = (item, options, tight, renderBlock) => {
|
|
4
|
+
const checkHtml = item.checked !== undefined
|
|
5
|
+
? `<input type="checkbox"${item.checked ? ' checked' : ''} disabled /> `
|
|
6
|
+
: ''
|
|
7
|
+
const taskClass = item.checked !== undefined ? ' class="task-list-item"' : ''
|
|
8
|
+
|
|
9
|
+
if (Array.isArray(item)) {
|
|
10
|
+
return `<li${taskClass}>${checkHtml}${renderInline(item, options)}</li>\n`
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const children = item.children || []
|
|
14
|
+
const childrenHtml = children
|
|
15
|
+
.map((child, idx) => {
|
|
16
|
+
if (child.type === 'paragraph' && tight && idx === 0) {
|
|
17
|
+
return renderInline(child.children, options)
|
|
18
|
+
}
|
|
19
|
+
return renderBlock(child, options)
|
|
20
|
+
})
|
|
21
|
+
.join('')
|
|
22
|
+
|
|
23
|
+
return `<li${taskClass}>${checkHtml}${childrenHtml}</li>\n`
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const renderList = (node, options, renderBlock) => {
|
|
27
|
+
const tight = node.tight !== false
|
|
28
|
+
const isOrdered = node.type === 'orderedList'
|
|
29
|
+
const listItems = node.children || node.items || []
|
|
30
|
+
const itemsHtml = listItems.map((item) => renderListItem(item, options, tight, renderBlock)).join('')
|
|
31
|
+
|
|
32
|
+
if (isOrdered) {
|
|
33
|
+
const startAttr = node.start && node.start !== 1 ? ` start="${node.start}"` : ''
|
|
34
|
+
return `<ol${startAttr}>\n${itemsHtml}</ol>\n`
|
|
35
|
+
}
|
|
36
|
+
return `<ul>\n${itemsHtml}</ul>\n`
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
module.exports = { renderList }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sullux/markdown-html",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A high-performance, bidirectional Markdown <-> HTML compiler supporting GFM, GitBook hints, syntax highlighting, and custom image dimensions.",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"author": "Charles Sullivan <charles@sullux.com>",
|
|
@@ -31,6 +31,6 @@
|
|
|
31
31
|
"registry": "https://registry.npmjs.org/"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@sullux/markdown-compiler": "^
|
|
34
|
+
"@sullux/markdown-compiler": "^2.0.0"
|
|
35
35
|
}
|
|
36
36
|
}
|