@magic/transmute 0.0.59 → 0.0.61
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.
- package/README.md +30 -16
- package/package.json +48 -41
- package/src/bin/handleArgv.js +4 -0
- package/src/bin/runCli.js +38 -24
- package/src/bin.js +2 -3
- package/src/index.js +17 -5
- package/src/lib/escape.js +1 -0
- package/src/lib/findState.js +4 -0
- package/src/lib/implantState.js +8 -2
- package/src/lib/stringDecode.js +1 -0
- package/src/renderers/html.js +63 -48
- package/src/renderers/markdown.js +46 -21
package/README.md
CHANGED
|
@@ -3,10 +3,6 @@
|
|
|
3
3
|
transmutes html and markdown to @magic view fragments
|
|
4
4
|
|
|
5
5
|
[![NPM version][npm-image]][npm-url]
|
|
6
|
-
[![Linux Build Status][travis-image]][travis-url]
|
|
7
|
-
[![Windows Build Status][appveyor-image]][appveyor-url]
|
|
8
|
-
[![Coverage Status][coveralls-image]][coveralls-url]
|
|
9
|
-
[![Known Vulnerabilities][snyk-image]][snyk-url]
|
|
10
6
|
|
|
11
7
|
### installation
|
|
12
8
|
|
|
@@ -40,7 +36,7 @@ npm i --save @magic/transmute
|
|
|
40
36
|
#### flags:
|
|
41
37
|
|
|
42
38
|
- --add-wrapper - add export default[] to the returned string. - alias: ["--addWrapper"]
|
|
43
|
-
- --no-pretty - do not run
|
|
39
|
+
- --no-pretty - do not run oxfmt. - alias: ["--noPretty"]
|
|
44
40
|
- --markdown - force markdown parser to run. - alias: ["--mark", "-m"]
|
|
45
41
|
- --output - output file path - alias: ["--out", "-o"]
|
|
46
42
|
- --input - input file path - alias: ["--in", "-i"]
|
|
@@ -343,17 +339,35 @@ update dependencies
|
|
|
343
339
|
|
|
344
340
|
- update dependencies
|
|
345
341
|
|
|
346
|
-
##### 0.0.59
|
|
342
|
+
##### 0.0.59
|
|
343
|
+
|
|
344
|
+
- update dependencies
|
|
345
|
+
|
|
346
|
+
##### 0.0.60
|
|
347
|
+
|
|
348
|
+
- replace prettier with oxfmt in runCli
|
|
349
|
+
- remove prettier dependency, add oxfmt 0.78.0
|
|
350
|
+
- update help text and docs to say 'do not run oxfmt'
|
|
351
|
+
- adjust test expectations for oxfmt
|
|
352
|
+
- exclude docs and example/pages/html.html from package
|
|
353
|
+
- add test coverage for state implantation through public API
|
|
354
|
+
- add unit tests for html and markdown renderers
|
|
355
|
+
- add tests for CLI argument handling, runCli and CLI itself
|
|
356
|
+
- add tests for implantState and stringDecode
|
|
357
|
+
- await oxfmt format in runCli
|
|
358
|
+
- make tsc and oxlint happy
|
|
359
|
+
- remove duplicate --output from CLI help
|
|
360
|
+
- format example files
|
|
361
|
+
- remove .travis and appveyor config files
|
|
362
|
+
- format package.json
|
|
363
|
+
- add @magic/lint, add npm run tasks for tsc and lint, add oxlint.config and tsconfig files
|
|
364
|
+
- update multiple dependencies
|
|
365
|
+
|
|
366
|
+
##### 0.0.61
|
|
367
|
+
|
|
368
|
+
- sort imports
|
|
369
|
+
- update dependencies
|
|
347
370
|
|
|
348
|
-
...
|
|
349
371
|
|
|
350
|
-
[npm-image]: https://img.shields.io/npm/v/@magic/transmute.
|
|
372
|
+
[npm-image]: https://img.shields.io/npm/v/@magic/transmute.sv
|
|
351
373
|
[npm-url]: https://www.npmjs.com/package/@magic/transmute
|
|
352
|
-
[travis-image]: https://img.shields.io/travis/com/magic/transmute/master
|
|
353
|
-
[travis-url]: https://travis-ci.com/magic/transmute
|
|
354
|
-
[appveyor-image]: https://img.shields.io/appveyor/ci/magic/transmute/master.svg
|
|
355
|
-
[appveyor-url]: https://ci.appveyor.com/project/magic/transmute/branch/master
|
|
356
|
-
[coveralls-image]: https://coveralls.io/repos/github/magic/transmute/badge.svg
|
|
357
|
-
[coveralls-url]: https://coveralls.io/github/magic/transmute
|
|
358
|
-
[snyk-image]: https://snyk.io/test/github/magic/transmute/badge.svg
|
|
359
|
-
[snyk-url]: https://snyk.io/test/github/magic/transmute
|
package/package.json
CHANGED
|
@@ -1,48 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magic/transmute",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"author": "Wizards & Witches",
|
|
5
|
-
"homepage": "https://magic.github.io/transmute",
|
|
6
|
-
"license": "AGPL-3.0",
|
|
3
|
+
"version": "0.0.61",
|
|
7
4
|
"description": "convert markdown and html to magic function strings.",
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
"files": [
|
|
14
|
-
"src"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"html",
|
|
7
|
+
"magic",
|
|
8
|
+
"markdown",
|
|
9
|
+
"transmute"
|
|
15
10
|
],
|
|
16
|
-
"
|
|
17
|
-
|
|
11
|
+
"homepage": "https://magic.github.io/transmute",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/magic/transmute/issues"
|
|
18
14
|
},
|
|
19
|
-
"
|
|
15
|
+
"license": "AGPL-3.0",
|
|
16
|
+
"author": "Wizards & Witches",
|
|
17
|
+
"contributors": [
|
|
18
|
+
{
|
|
19
|
+
"name": "jascha ehrenreich",
|
|
20
|
+
"github": "jaeh"
|
|
21
|
+
}
|
|
22
|
+
],
|
|
20
23
|
"repository": {
|
|
21
24
|
"type": "git",
|
|
22
25
|
"url": "git+https://github.com/magic/transmute.git"
|
|
23
26
|
},
|
|
24
|
-
"
|
|
25
|
-
"
|
|
27
|
+
"bin": {
|
|
28
|
+
"magic-transmute": "src/bin.js"
|
|
26
29
|
},
|
|
30
|
+
"files": [
|
|
31
|
+
"src"
|
|
32
|
+
],
|
|
33
|
+
"type": "module",
|
|
34
|
+
"main": "src/index.js",
|
|
27
35
|
"scripts": {
|
|
28
36
|
"start": "t -p",
|
|
29
37
|
"test": "t",
|
|
30
|
-
"format": "f -w
|
|
38
|
+
"format": "f -w '!docs' '!example/pages/html.html'",
|
|
31
39
|
"build": "NODE_ENV=production magic build",
|
|
32
40
|
"prod": "NODE_ENV=production magic build serve",
|
|
33
41
|
"dev": "magic dev",
|
|
34
42
|
"dirty-cli-test": "src/bin/bin.js file --input test/.data/testing.md --output testing.js && echo 'contents: ' && cat testing.js && echo 'deleting testing.js' && rm testing.js",
|
|
35
|
-
"
|
|
43
|
+
"lint": "lint",
|
|
44
|
+
"tsc": "tsc --noEmit",
|
|
45
|
+
"tsc:test": "tsc --noEmit -p tsconfig.test.json",
|
|
46
|
+
"check": "npm run tsc && npm run tsc:test && npm run lint"
|
|
36
47
|
},
|
|
37
48
|
"dependencies": {
|
|
38
|
-
"@magic/cases": "0.0.
|
|
39
|
-
"@magic/cli": "0.0.
|
|
40
|
-
"@magic/entities": "0.0.
|
|
41
|
-
"@magic/log": "0.1.
|
|
42
|
-
"@magic/types": "0.1.
|
|
43
|
-
"marked": "18.
|
|
44
|
-
"
|
|
45
|
-
"
|
|
49
|
+
"@magic/cases": "0.0.15",
|
|
50
|
+
"@magic/cli": "0.0.56",
|
|
51
|
+
"@magic/entities": "0.0.19",
|
|
52
|
+
"@magic/log": "0.1.26",
|
|
53
|
+
"@magic/types": "0.1.42",
|
|
54
|
+
"marked": "18.2.1",
|
|
55
|
+
"oxfmt": "0.79.0",
|
|
56
|
+
"posthtml-parser": "0.13.0"
|
|
46
57
|
},
|
|
47
58
|
"devDependencies": {
|
|
48
59
|
"@magic-modules/git-badges": "0.0.12",
|
|
@@ -50,20 +61,16 @@
|
|
|
50
61
|
"@magic-modules/no-spy": "0.0.9",
|
|
51
62
|
"@magic-modules/pre": "0.0.12",
|
|
52
63
|
"@magic-themes/docs": "0.0.15",
|
|
53
|
-
"@magic/core": "0.0.
|
|
54
|
-
"@magic/format": "0.0.
|
|
55
|
-
"@magic/
|
|
64
|
+
"@magic/core": "0.0.161",
|
|
65
|
+
"@magic/format": "0.0.82",
|
|
66
|
+
"@magic/lint": "0.0.6",
|
|
67
|
+
"@magic/test": "0.3.59"
|
|
56
68
|
},
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
{
|
|
65
|
-
"name": "jascha ehrenreich",
|
|
66
|
-
"github": "jaeh"
|
|
67
|
-
}
|
|
68
|
-
]
|
|
69
|
+
"engines": {
|
|
70
|
+
"node": ">=14.15.4"
|
|
71
|
+
},
|
|
72
|
+
"engineStrict": true,
|
|
73
|
+
"allowScripts": {
|
|
74
|
+
"@swc/core@1.15.47": true
|
|
75
|
+
}
|
|
69
76
|
}
|
package/src/bin/handleArgv.js
CHANGED
package/src/bin/runCli.js
CHANGED
|
@@ -1,40 +1,54 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
1
|
import fs from 'fs'
|
|
3
|
-
|
|
4
|
-
import prettier from 'prettier'
|
|
2
|
+
import path from 'path'
|
|
5
3
|
|
|
6
4
|
import log from '@magic/log'
|
|
7
5
|
import is from '@magic/types'
|
|
8
6
|
|
|
7
|
+
import { format } from 'oxfmt'
|
|
9
8
|
import transmute from '../index.js'
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
10
|
+
// oxfmt's format returns a promise, so the pretty path is async.
|
|
11
|
+
/**
|
|
12
|
+
* @param {string} source
|
|
13
|
+
* @returns {Promise<string>}
|
|
14
|
+
*/
|
|
15
|
+
const formatCode = async source => {
|
|
16
|
+
const result = await format('script.js', source, {
|
|
17
|
+
semi: false,
|
|
18
|
+
singleQuote: true,
|
|
19
|
+
trailingComma: 'es5',
|
|
20
|
+
})
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
return result.code
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {string} html
|
|
27
|
+
* @param {boolean} isMarkdown
|
|
28
|
+
* @param {import('@magic/cli').ParsedCLI} res
|
|
29
|
+
*/
|
|
30
|
+
export const runCli = async (html, isMarkdown, res) => {
|
|
31
|
+
const out = isMarkdown ? transmute.markdown(html) : transmute.html(html)
|
|
32
|
+
|
|
33
|
+
let rendered = out.rendered
|
|
34
|
+
|
|
35
|
+
if (is.array(rendered)) {
|
|
36
|
+
if (rendered.length === 1) {
|
|
37
|
+
rendered = rendered[0]
|
|
22
38
|
} else {
|
|
23
|
-
|
|
39
|
+
rendered = `[${rendered.join(',')}]`
|
|
24
40
|
}
|
|
25
41
|
}
|
|
26
42
|
|
|
43
|
+
/** @type {string} */
|
|
44
|
+
let outString = rendered
|
|
45
|
+
|
|
27
46
|
if (res.args.addWrapper) {
|
|
28
|
-
|
|
47
|
+
outString = `export default [\n${outString},\n]`
|
|
29
48
|
}
|
|
30
49
|
|
|
31
50
|
if (!res.args.noPretty) {
|
|
32
|
-
|
|
33
|
-
semi: false,
|
|
34
|
-
parser: 'babel',
|
|
35
|
-
singleQuote: true,
|
|
36
|
-
trailingComma: 'es5',
|
|
37
|
-
})
|
|
51
|
+
outString = await formatCode(outString)
|
|
38
52
|
}
|
|
39
53
|
|
|
40
54
|
if (res.args.output) {
|
|
@@ -42,12 +56,12 @@ export const runCli = (html, isMarkdown, res) => {
|
|
|
42
56
|
const dir = path.dirname(outputFile)
|
|
43
57
|
if (!fs.existsSync(dir)) {
|
|
44
58
|
log.info('output dir does not exist, creating it.', dir)
|
|
45
|
-
|
|
59
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
46
60
|
}
|
|
47
61
|
|
|
48
62
|
log.info('writing to:', { outputFile })
|
|
49
|
-
fs.writeFileSync(outputFile,
|
|
63
|
+
fs.writeFileSync(outputFile, outString)
|
|
50
64
|
} else {
|
|
51
|
-
log(
|
|
65
|
+
log(outString)
|
|
52
66
|
}
|
|
53
67
|
}
|
package/src/bin.js
CHANGED
|
@@ -26,10 +26,9 @@ const args = {
|
|
|
26
26
|
options: {
|
|
27
27
|
'--output': 'output file to write to',
|
|
28
28
|
'--add-wrapper': 'add export default[] to the returned string.',
|
|
29
|
-
'--no-pretty': 'do not run
|
|
29
|
+
'--no-pretty': 'do not run oxfmt.',
|
|
30
30
|
'--markdown': 'force markdown parser to run.',
|
|
31
31
|
'--input': 'input file path',
|
|
32
|
-
'--output': 'output file path',
|
|
33
32
|
'--help': 'this help text',
|
|
34
33
|
'--str': 'an input string of either html or markdown, depending on running command',
|
|
35
34
|
},
|
|
@@ -56,4 +55,4 @@ const res = cli(args)
|
|
|
56
55
|
|
|
57
56
|
const [html, isMarkdown] = handleArgv(res)
|
|
58
57
|
|
|
59
|
-
runCli(html, isMarkdown, res)
|
|
58
|
+
await runCli(html, isMarkdown, res)
|
package/src/index.js
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
|
-
import { marked } from 'marked'
|
|
2
|
-
import { parser } from 'posthtml-parser'
|
|
3
|
-
|
|
4
1
|
import is from '@magic/types'
|
|
5
2
|
|
|
3
|
+
import { marked } from 'marked'
|
|
4
|
+
import { parser } from 'posthtml-parser'
|
|
6
5
|
import { findState, implantState } from './lib/index.js'
|
|
7
|
-
|
|
8
6
|
import * as renderers from './renderers/index.js'
|
|
9
7
|
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {Record<string, unknown>} State
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} string
|
|
14
|
+
* @param {State} [state]
|
|
15
|
+
* @param {object} [originalState]
|
|
16
|
+
*/
|
|
10
17
|
export const markdown = (string, state = {}, originalState = {}) => {
|
|
11
18
|
const { input, state: st = {} } = findState(string)
|
|
12
19
|
|
|
@@ -21,13 +28,18 @@ export const markdown = (string, state = {}, originalState = {}) => {
|
|
|
21
28
|
}
|
|
22
29
|
|
|
23
30
|
const implanted = implantState({ input: string, state })
|
|
24
|
-
|
|
31
|
+
const md = marked(implanted, { renderer: renderers.markdown, async: false })
|
|
25
32
|
|
|
26
33
|
const out = html(md, state, originalState)
|
|
27
34
|
|
|
28
35
|
return out
|
|
29
36
|
}
|
|
30
37
|
|
|
38
|
+
/**
|
|
39
|
+
* @param {string} string
|
|
40
|
+
* @param {State} [state]
|
|
41
|
+
* @param {object} [originalState]
|
|
42
|
+
*/
|
|
31
43
|
export const html = (string, state = {}, originalState = {}) => {
|
|
32
44
|
const { input, state: st = {} } = findState(string)
|
|
33
45
|
|
package/src/lib/escape.js
CHANGED
package/src/lib/findState.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
// find and extract the page state from both markdown and html vars.
|
|
2
|
+
/**
|
|
3
|
+
* @param {string} input
|
|
4
|
+
* @returns {{ input: string, state?: Record<string, unknown> }}
|
|
5
|
+
*/
|
|
2
6
|
export const findState = input => {
|
|
3
7
|
if (input.trim().startsWith('---')) {
|
|
4
8
|
const splinters = input.split('---')
|
package/src/lib/implantState.js
CHANGED
|
@@ -2,6 +2,10 @@ import is from '@magic/types'
|
|
|
2
2
|
|
|
3
3
|
const bracketRegExp = /(\${|{{).*(}|}})/gim
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* @param {{ input: string, state: Record<string, unknown> }} params
|
|
7
|
+
* @returns {string}
|
|
8
|
+
*/
|
|
5
9
|
export const implantState = ({ input, state }) => {
|
|
6
10
|
const matches = input.match(bracketRegExp)
|
|
7
11
|
|
|
@@ -26,7 +30,9 @@ export const implantState = ({ input, state }) => {
|
|
|
26
30
|
|
|
27
31
|
subMatches.forEach(subM => {
|
|
28
32
|
if (is.undefined(curState[subM])) {
|
|
29
|
-
curState =
|
|
33
|
+
curState = /** @type {Record<string, unknown>} */ (
|
|
34
|
+
/** @type {unknown} */ (curState[subM])
|
|
35
|
+
)
|
|
30
36
|
val = curState
|
|
31
37
|
} else {
|
|
32
38
|
val = undefined
|
|
@@ -46,7 +52,7 @@ export const implantState = ({ input, state }) => {
|
|
|
46
52
|
val = val.join(' ')
|
|
47
53
|
}
|
|
48
54
|
|
|
49
|
-
input = input.replace(match, val)
|
|
55
|
+
input = input.replace(match, /** @type {string} */ (val))
|
|
50
56
|
})
|
|
51
57
|
}
|
|
52
58
|
|
package/src/lib/stringDecode.js
CHANGED
package/src/renderers/html.js
CHANGED
|
@@ -3,16 +3,26 @@ import is from '@magic/types'
|
|
|
3
3
|
|
|
4
4
|
import { stringDecode } from '../lib/index.js'
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('posthtml-parser').NodeTag} NodeTag
|
|
8
|
+
* @typedef {string | number | NodeTag} NodeValue
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {Array<NodeValue>} content
|
|
13
|
+
* @returns {string | Array<string>}
|
|
14
|
+
*/
|
|
6
15
|
const handleContent = content => {
|
|
7
16
|
if (is.empty(content)) {
|
|
8
17
|
return ''
|
|
9
18
|
}
|
|
10
19
|
|
|
11
|
-
|
|
20
|
+
/** @type {Array<NodeValue> | string} */
|
|
21
|
+
let out = content.filter(a => (!is.string(a) ? a : a.trim()))
|
|
12
22
|
|
|
13
|
-
if (is.array(
|
|
14
|
-
if (
|
|
15
|
-
let cnt =
|
|
23
|
+
if (is.array(out)) {
|
|
24
|
+
if (out.length === 1) {
|
|
25
|
+
let cnt = out[0]
|
|
16
26
|
if (is.string(cnt)) {
|
|
17
27
|
cnt = stringDecode(cnt)
|
|
18
28
|
}
|
|
@@ -20,55 +30,55 @@ const handleContent = content => {
|
|
|
20
30
|
return html(cnt)
|
|
21
31
|
}
|
|
22
32
|
|
|
23
|
-
|
|
33
|
+
out = `[${out.map(html).filter(a => a)}]`
|
|
24
34
|
}
|
|
25
35
|
|
|
26
|
-
|
|
27
|
-
content = ''
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (is.string(content)) {
|
|
31
|
-
return stringDecode(content)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return content
|
|
36
|
+
return stringDecode(out)
|
|
35
37
|
}
|
|
36
38
|
|
|
39
|
+
/**
|
|
40
|
+
* @param {Record<string, string | number | boolean> | undefined} attrs
|
|
41
|
+
* @param {string | boolean | undefined} tag
|
|
42
|
+
* @returns {string}
|
|
43
|
+
*/
|
|
37
44
|
const handleAttrs = (attrs, tag) => {
|
|
38
|
-
if (attrs) {
|
|
39
|
-
|
|
40
|
-
.map(([key, val]) => {
|
|
41
|
-
val = val.trim()
|
|
42
|
-
|
|
43
|
-
if (tag === 'a' || tag === 'Link') {
|
|
44
|
-
if (key === 'href') {
|
|
45
|
-
key = 'to'
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
key = cases.camel(key)
|
|
50
|
-
|
|
51
|
-
if (!val) {
|
|
52
|
-
return key
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (val.startsWith('[') || val.startsWith('{')) {
|
|
56
|
-
return `${key}: ${val}`
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return `${key}: '${val}'`
|
|
60
|
-
})
|
|
61
|
-
.filter(a => a)
|
|
62
|
-
.join(', ')
|
|
63
|
-
|
|
64
|
-
attrs = `{ ${attrs} }`
|
|
65
|
-
} else {
|
|
66
|
-
attrs = ''
|
|
45
|
+
if (!attrs) {
|
|
46
|
+
return ''
|
|
67
47
|
}
|
|
68
48
|
|
|
69
|
-
|
|
49
|
+
const out = Object.entries(attrs)
|
|
50
|
+
.map(([key, rawVal]) => {
|
|
51
|
+
const val = /** @type {string} */ (rawVal)
|
|
52
|
+
const trimmed = val.trim()
|
|
53
|
+
|
|
54
|
+
let outKey = key
|
|
55
|
+
|
|
56
|
+
if ((tag === 'a' || tag === 'Link') && outKey === 'href') {
|
|
57
|
+
outKey = 'to'
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
outKey = cases.camel(outKey)
|
|
61
|
+
|
|
62
|
+
if (!trimmed) {
|
|
63
|
+
return outKey
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (trimmed.startsWith('[') || trimmed.startsWith('{')) {
|
|
67
|
+
return `${outKey}: ${trimmed}`
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return `${outKey}: '${trimmed}'`
|
|
71
|
+
})
|
|
72
|
+
.filter(a => a)
|
|
73
|
+
.join(', ')
|
|
74
|
+
|
|
75
|
+
return `{ ${out} }`
|
|
70
76
|
}
|
|
71
77
|
|
|
78
|
+
/**
|
|
79
|
+
* @param {NodeValue | Array<NodeValue>} ast
|
|
80
|
+
* @returns {string | Array<string>}
|
|
81
|
+
*/
|
|
72
82
|
export const html = ast => {
|
|
73
83
|
if (is.string(ast)) {
|
|
74
84
|
if (!ast.trim()) {
|
|
@@ -98,11 +108,13 @@ export const html = ast => {
|
|
|
98
108
|
}
|
|
99
109
|
|
|
100
110
|
if (is.objectNative(ast)) {
|
|
101
|
-
|
|
111
|
+
const node = /** @type {NodeTag} */ (ast)
|
|
112
|
+
let { tag } = node
|
|
113
|
+
const rawContent = node.content
|
|
102
114
|
|
|
103
|
-
content = handleContent(
|
|
115
|
+
let content = handleContent(/** @type {Array<NodeValue>} */ (rawContent))
|
|
104
116
|
|
|
105
|
-
attrs = handleAttrs(attrs, tag)
|
|
117
|
+
let attrs = handleAttrs(node.attrs, tag)
|
|
106
118
|
|
|
107
119
|
if (attrs && !is.empty(content)) {
|
|
108
120
|
attrs += ', '
|
|
@@ -135,5 +147,8 @@ export const html = ast => {
|
|
|
135
147
|
return `${tag}(${attrs}${content})`
|
|
136
148
|
}
|
|
137
149
|
|
|
138
|
-
|
|
150
|
+
const nodes = /** @type {Array<NodeValue>} */ (ast)
|
|
151
|
+
const out = /** @type {Array<string>} */ (nodes.map(html))
|
|
152
|
+
|
|
153
|
+
return out.filter(a => a)
|
|
139
154
|
}
|
|
@@ -4,8 +4,9 @@ export const markdown = new Renderer()
|
|
|
4
4
|
|
|
5
5
|
// Slugger implementation (removed from marked v5+)
|
|
6
6
|
// No deduplication - reset state for each slugify call
|
|
7
|
+
/** @param {string} str */
|
|
7
8
|
const slugify = str => {
|
|
8
|
-
|
|
9
|
+
const s = str
|
|
9
10
|
.toLowerCase()
|
|
10
11
|
.trim()
|
|
11
12
|
.replace(/[\s]+/g, '-')
|
|
@@ -18,9 +19,9 @@ const slugify = str => {
|
|
|
18
19
|
|
|
19
20
|
/* overwrite builtin markdown functions to use @magic modules */
|
|
20
21
|
|
|
21
|
-
markdown.code = token => {
|
|
22
|
+
markdown.code = (/** @type {import('marked').Tokens.Code} */ token) => {
|
|
22
23
|
const { text, lang } = token
|
|
23
|
-
const language = (lang || '').match(/\S*/)[0]
|
|
24
|
+
const language = (lang || '').match(/\S*/)?.[0]
|
|
24
25
|
|
|
25
26
|
if (!language) {
|
|
26
27
|
return `<Pre>${text}</Pre>\n`
|
|
@@ -29,9 +30,10 @@ markdown.code = token => {
|
|
|
29
30
|
return `<Pre class="${language}">${text}</Pre>`
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
markdown.codespan =
|
|
33
|
+
markdown.codespan = (/** @type {import('marked').Tokens.Codespan} */ token) =>
|
|
34
|
+
`<Pre lines="false">${token.text}</Pre>`
|
|
33
35
|
|
|
34
|
-
markdown.link = token => {
|
|
36
|
+
markdown.link = (/** @type {import('marked').Tokens.Link} */ token) => {
|
|
35
37
|
const { href, title, text } = token
|
|
36
38
|
if (href === null) {
|
|
37
39
|
return text
|
|
@@ -52,7 +54,7 @@ markdown.link = token => {
|
|
|
52
54
|
return out
|
|
53
55
|
}
|
|
54
56
|
|
|
55
|
-
markdown.image = token => {
|
|
57
|
+
markdown.image = (/** @type {import('marked').Tokens.Image} */ token) => {
|
|
56
58
|
const { href, title, text } = token
|
|
57
59
|
if (href === null) {
|
|
58
60
|
return text
|
|
@@ -73,13 +75,16 @@ markdown.image = token => {
|
|
|
73
75
|
return out
|
|
74
76
|
}
|
|
75
77
|
|
|
76
|
-
markdown.heading = token => {
|
|
77
|
-
let { text
|
|
78
|
+
markdown.heading = (/** @type {import('marked').Tokens.Heading} */ token) => {
|
|
79
|
+
let { text } = token
|
|
80
|
+
const depth = token.depth
|
|
78
81
|
let id = slugify(text)
|
|
79
82
|
let cssClass = ''
|
|
80
83
|
|
|
81
84
|
if (text.startsWith('#')) {
|
|
82
|
-
|
|
85
|
+
const parts = text.split(' ')
|
|
86
|
+
let setId = parts[0]
|
|
87
|
+
const t = parts.slice(1)
|
|
83
88
|
|
|
84
89
|
text = t.join(' ')
|
|
85
90
|
|
|
@@ -91,7 +96,9 @@ markdown.heading = token => {
|
|
|
91
96
|
}
|
|
92
97
|
|
|
93
98
|
if (text.startsWith('.')) {
|
|
94
|
-
|
|
99
|
+
const parts = text.split(' ')
|
|
100
|
+
let setClass = parts[0]
|
|
101
|
+
const t = parts.slice(1)
|
|
95
102
|
|
|
96
103
|
text = t.join(' ')
|
|
97
104
|
|
|
@@ -105,7 +112,9 @@ markdown.heading = token => {
|
|
|
105
112
|
return `<h${depth} id="${id}"${cssClass}>${text}</h${depth}>\n`
|
|
106
113
|
}
|
|
107
114
|
|
|
108
|
-
markdown.html =
|
|
115
|
+
markdown.html = (
|
|
116
|
+
/** @type {import('marked').Tokens.HTML | import('marked').Tokens.Tag} */ token,
|
|
117
|
+
) => {
|
|
109
118
|
let html = token.text || token.raw
|
|
110
119
|
if (html.startsWith('<img')) {
|
|
111
120
|
html = html.replace('<img', '<Img')
|
|
@@ -122,8 +131,9 @@ markdown.html = token => {
|
|
|
122
131
|
return html
|
|
123
132
|
}
|
|
124
133
|
|
|
125
|
-
markdown.paragraph = token => {
|
|
134
|
+
markdown.paragraph = (/** @type {import('marked').Tokens.Paragraph} */ token) => {
|
|
126
135
|
const text = token.text || ''
|
|
136
|
+
/** @type {import('marked').Tokens.Generic[]} */
|
|
127
137
|
const inlineTokens = token.tokens || []
|
|
128
138
|
|
|
129
139
|
// Check if single token that shouldn't be wrapped in <p>
|
|
@@ -131,14 +141,20 @@ markdown.paragraph = token => {
|
|
|
131
141
|
const t = inlineTokens[0]
|
|
132
142
|
if (t.type === 'link') {
|
|
133
143
|
let out = `<Link to="${t.href}"`
|
|
134
|
-
if (t.title)
|
|
144
|
+
if (t.title) {
|
|
145
|
+
out += ` title="${t.title}"`
|
|
146
|
+
}
|
|
135
147
|
out += ` text="${t.text}"></Link>`
|
|
136
148
|
return out + '\n'
|
|
137
149
|
}
|
|
138
150
|
if (t.type === 'image') {
|
|
139
151
|
let out = `<Img src="${t.href}"`
|
|
140
|
-
if (t.text)
|
|
141
|
-
|
|
152
|
+
if (t.text) {
|
|
153
|
+
out += ` alt="${t.text}"`
|
|
154
|
+
}
|
|
155
|
+
if (t.title) {
|
|
156
|
+
out += ` title="${t.title}"`
|
|
157
|
+
}
|
|
142
158
|
return out + '/>\n'
|
|
143
159
|
}
|
|
144
160
|
if (t.type === 'codespan') {
|
|
@@ -174,17 +190,22 @@ markdown.paragraph = token => {
|
|
|
174
190
|
}
|
|
175
191
|
|
|
176
192
|
// Render inline tokens
|
|
177
|
-
|
|
193
|
+
/** @returns {string} */
|
|
194
|
+
const renderInline = (/** @type {import('marked').Tokens.Generic[]} */ tokens) => {
|
|
178
195
|
return tokens
|
|
179
196
|
.map(t => {
|
|
180
197
|
if (t.type === 'link') {
|
|
181
198
|
let out = `<Link to="${t.href}"`
|
|
182
|
-
if (t.title)
|
|
199
|
+
if (t.title) {
|
|
200
|
+
out += ` title="${t.title}"`
|
|
201
|
+
}
|
|
183
202
|
out += ` text="${t.text}"></Link>`
|
|
184
203
|
return out
|
|
185
204
|
}
|
|
186
205
|
if (t.type === 'strong' || t.type === 'em') {
|
|
187
|
-
const inner = t.tokens
|
|
206
|
+
const inner = t.tokens
|
|
207
|
+
? renderInline(/** @type {import('marked').Tokens.Generic[]} */ t.tokens)
|
|
208
|
+
: t.text
|
|
188
209
|
const tag = t.type === 'strong' ? 'b' : 'em'
|
|
189
210
|
return `<${tag}>${inner}</${tag}>`
|
|
190
211
|
}
|
|
@@ -193,8 +214,12 @@ markdown.paragraph = token => {
|
|
|
193
214
|
}
|
|
194
215
|
if (t.type === 'image') {
|
|
195
216
|
let out = `<Img src="${t.href}"`
|
|
196
|
-
if (t.text)
|
|
197
|
-
|
|
217
|
+
if (t.text) {
|
|
218
|
+
out += ` alt="${t.text}"`
|
|
219
|
+
}
|
|
220
|
+
if (t.title) {
|
|
221
|
+
out += ` title="${t.title}"`
|
|
222
|
+
}
|
|
198
223
|
out += '/>'
|
|
199
224
|
return out
|
|
200
225
|
}
|
|
@@ -219,4 +244,4 @@ markdown.paragraph = token => {
|
|
|
219
244
|
return `<p>${rendered}</p>\n`
|
|
220
245
|
}
|
|
221
246
|
|
|
222
|
-
markdown.strong = token => `<b>${token.text}</b>`
|
|
247
|
+
markdown.strong = (/** @type {import('marked').Tokens.Strong} */ token) => `<b>${token.text}</b>`
|