@tamagui/code-to-html 1.0.1-beta.142
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/dist/cjs/highlightLine.js +121 -0
- package/dist/cjs/highlightLine.js.map +7 -0
- package/dist/cjs/highlightWord.js +43 -0
- package/dist/cjs/highlightWord.js.map +7 -0
- package/dist/cjs/index.js +151 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/esm/highlightLine.js +9868 -0
- package/dist/esm/highlightLine.js.map +7 -0
- package/dist/esm/highlightWord.js +9790 -0
- package/dist/esm/highlightWord.js.map +7 -0
- package/dist/esm/index.js +16314 -0
- package/dist/esm/index.js.map +7 -0
- package/package.json +38 -0
- package/src/highlightLine.ts +112 -0
- package/src/highlightWord.ts +13 -0
- package/src/index.ts +19 -0
- package/types/highlightLine.d.ts +2 -0
- package/types/highlightWord.d.ts +2 -0
- package/types/index.d.ts +2 -0
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tamagui/code-to-html",
|
|
3
|
+
"version": "1.0.1-beta.142",
|
|
4
|
+
"source": "src/index.ts",
|
|
5
|
+
"types": "./types/index.d.ts",
|
|
6
|
+
"main": "dist/cjs",
|
|
7
|
+
"module": "dist/esm",
|
|
8
|
+
"files": [
|
|
9
|
+
"src",
|
|
10
|
+
"types",
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tamagui-build --bundle",
|
|
15
|
+
"watch": "tamagui-build --bundle --watch",
|
|
16
|
+
"lint": "eslint",
|
|
17
|
+
"lint:fix": "yarn lint --fix",
|
|
18
|
+
"clean": "tamagui-build clean",
|
|
19
|
+
"clean:build": "tamagui-build clean:build"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"hast-util-to-html": "^8.0.3",
|
|
23
|
+
"hast-util-to-string": "^2.0.0",
|
|
24
|
+
"parse-numeric-range": "^1.3.0",
|
|
25
|
+
"refractor": "^4.7.0",
|
|
26
|
+
"rehype": "^12.0.1",
|
|
27
|
+
"rehype-parse": "^8.0.4",
|
|
28
|
+
"rehype-slug": "^5.0.1",
|
|
29
|
+
"unified": "^10.1.2",
|
|
30
|
+
"url": "^0.11.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@tamagui/build": "^1.0.1-beta.142"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// Inspired by https://github.com/j0lv3r4/mdx-prism
|
|
2
|
+
|
|
3
|
+
import { toHtml } from 'hast-util-to-html'
|
|
4
|
+
import parse from 'rehype-parse'
|
|
5
|
+
import { unified } from 'unified'
|
|
6
|
+
|
|
7
|
+
const lineNumberify = function lineNumberify(ast, lineNum = 1) {
|
|
8
|
+
let lineNumber = lineNum
|
|
9
|
+
return ast.reduce(
|
|
10
|
+
(result, node) => {
|
|
11
|
+
if (node.type === 'text') {
|
|
12
|
+
if (node.value.indexOf('\n') === -1) {
|
|
13
|
+
node.lineNumber = lineNumber
|
|
14
|
+
result.nodes.push(node)
|
|
15
|
+
return result
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const lines = node.value.split('\n')
|
|
19
|
+
for (let i = 0; i < lines.length; i++) {
|
|
20
|
+
if (i !== 0) ++lineNumber
|
|
21
|
+
if (i === lines.length - 1 && lines[i].length === 0) continue
|
|
22
|
+
result.nodes.push({
|
|
23
|
+
type: 'text',
|
|
24
|
+
value: i === lines.length - 1 ? lines[i] : `${lines[i]}\n`,
|
|
25
|
+
lineNumber: lineNumber,
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
result.lineNumber = lineNumber
|
|
30
|
+
return result
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (node.children) {
|
|
34
|
+
node.lineNumber = lineNumber
|
|
35
|
+
const processed = lineNumberify(node.children, lineNumber)
|
|
36
|
+
node.children = processed.nodes
|
|
37
|
+
result.lineNumber = processed.lineNumber
|
|
38
|
+
result.nodes.push(node)
|
|
39
|
+
return result
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
result.nodes.push(node)
|
|
43
|
+
return result
|
|
44
|
+
},
|
|
45
|
+
{ nodes: [], lineNumber: lineNumber }
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const wrapLines = function wrapLines(ast: any[], linesToHighlight) {
|
|
50
|
+
const highlightAll = linesToHighlight.length === 1 && linesToHighlight[0] === 0
|
|
51
|
+
const allLines: any[] = Array.from(new Set(ast.map((x) => x.lineNumber)))
|
|
52
|
+
let i = 0
|
|
53
|
+
const wrapped = allLines.reduce((nodes, marker) => {
|
|
54
|
+
const line = marker
|
|
55
|
+
const children: any[] = []
|
|
56
|
+
for (; i < ast.length; i++) {
|
|
57
|
+
if (ast[i].lineNumber < line) {
|
|
58
|
+
nodes.push(ast[i])
|
|
59
|
+
continue
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (ast[i].lineNumber === line) {
|
|
63
|
+
children.push(ast[i])
|
|
64
|
+
continue
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (ast[i].lineNumber > line) {
|
|
68
|
+
break
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
nodes.push({
|
|
73
|
+
type: 'element',
|
|
74
|
+
tagName: 'div',
|
|
75
|
+
properties: {
|
|
76
|
+
dataLine: line,
|
|
77
|
+
className: 'highlight-line',
|
|
78
|
+
dataHighlighted: linesToHighlight.includes(line) || highlightAll ? 'true' : 'false',
|
|
79
|
+
},
|
|
80
|
+
children,
|
|
81
|
+
lineNumber: line,
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
return nodes
|
|
85
|
+
}, [])
|
|
86
|
+
|
|
87
|
+
return wrapped
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// https://github.com/gatsbyjs/gatsby/pull/26161/files
|
|
91
|
+
const MULTILINE_TOKEN_SPAN = /<span class="token ([^"]+)">[^<]*\n[^<]*<\/span>/g
|
|
92
|
+
|
|
93
|
+
const applyMultilineFix = function (ast) {
|
|
94
|
+
// AST to HTML
|
|
95
|
+
let html = toHtml(ast)
|
|
96
|
+
|
|
97
|
+
// Fix JSX issue
|
|
98
|
+
html = html.replace(MULTILINE_TOKEN_SPAN, (match, token) =>
|
|
99
|
+
match.replace(/\n/g, `</span>\n<span class="token ${token}">`)
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
// HTML to AST
|
|
103
|
+
const hast = unified().use(parse, { emitParseErrors: true, fragment: true }).parse(html)
|
|
104
|
+
|
|
105
|
+
return hast.children
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function highlightLine(ast, lines) {
|
|
109
|
+
const formattedAst = applyMultilineFix(ast)
|
|
110
|
+
const numbered = lineNumberify(formattedAst).nodes
|
|
111
|
+
return wrapLines(numbered, lines)
|
|
112
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// const visit = require('unist-util-visit')
|
|
2
|
+
import { toHtml } from 'hast-util-to-html'
|
|
3
|
+
import parse from 'rehype-parse'
|
|
4
|
+
import { unified } from 'unified'
|
|
5
|
+
|
|
6
|
+
const CALLOUT = /__(.*?)__/g
|
|
7
|
+
|
|
8
|
+
export function highlightWord(code) {
|
|
9
|
+
const html = toHtml(code)
|
|
10
|
+
const result = html.replace(CALLOUT, (_, text) => `<span class="highlight-word">${text}</span>`)
|
|
11
|
+
const hast = unified().use(parse, { emitParseErrors: true, fragment: true }).parse(result)
|
|
12
|
+
return hast.children
|
|
13
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { toHtml } from 'hast-util-to-html'
|
|
2
|
+
import rangeParser from 'parse-numeric-range'
|
|
3
|
+
import { refractor } from 'refractor'
|
|
4
|
+
import css from 'refractor/lang/css'
|
|
5
|
+
import tsx from 'refractor/lang/tsx'
|
|
6
|
+
|
|
7
|
+
import { highlightLine } from './highlightLine'
|
|
8
|
+
import { highlightWord } from './highlightWord'
|
|
9
|
+
|
|
10
|
+
refractor.register(tsx)
|
|
11
|
+
refractor.register(css)
|
|
12
|
+
|
|
13
|
+
export function codeToHTML(source: string, language: 'tsx' | 'css' | string, line = '0') {
|
|
14
|
+
let result: any = refractor.highlight(source, language)
|
|
15
|
+
result = highlightLine(result, rangeParser(line))
|
|
16
|
+
result = highlightWord(result)
|
|
17
|
+
result = toHtml(result)
|
|
18
|
+
return result as string
|
|
19
|
+
}
|
package/types/index.d.ts
ADDED