Skip to content

Commit 16f83d5

Browse files
author
李嘉宾
committed
update
1 parent f735b1b commit 16f83d5

File tree

6 files changed

+89
-28
lines changed

6 files changed

+89
-28
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
## 例子
1010

1111
[auto](https://listen80.github.io/code-view/examples/auto.html)
12-
12+
[element](https://listen80.github.io/code-view/examples/auto.html)
13+
[string](https://listen80.github.io/code-view/examples/string.html)
1314

1415
## 安装
1516

dist/code.min.js

Lines changed: 19 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/code.min.js.map

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

examples/element.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>element</title>
6+
</head>
7+
<body>
8+
<xmp id="foo">
9+
// javascript
10+
const a = 10
11+
12+
const b = new Date()
13+
14+
console.log(typeof window)
15+
16+
class A {
17+
constructor() {
18+
// something
19+
}
20+
}
21+
</xmp>
22+
<link rel="stylesheet" href="../dist/code.css">
23+
<script src="../dist/code.min.js" code></script>
24+
<script>
25+
const result = code(foo)
26+
console.log(result)
27+
</script>
28+
</body>
29+
</html>
30+

examples/string.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta charset="UTF-8" />
6+
<title>string</title>
7+
</head>
8+
<body>
9+
<link rel="stylesheet" href="../dist/code.css">
10+
<script src="../dist/code.min.js" code></script>
11+
<script>
12+
const result = code('let a = 3')
13+
document.write(result.ast)
14+
document.write(result.lan)
15+
console.log(result)
16+
</script>
17+
</body>
18+
</html>

src/index.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,30 @@
22
import { analysisForString, analysisForElement, analysisForTagName } from './analysis.js';
33
// import { createElement, createHtml } from './utils/domUtils.js';
44
function code(element, indent) {
5-
if (!element) {
6-
return [
7-
...analysisForTagName('code'),
8-
...analysisForTagName('pre'),
9-
...analysisForTagName('script'),
10-
...analysisForTagName('xmp')
11-
];
12-
} else {
13-
if (element instanceof Node) {
14-
return analysisForElement(element);
15-
} else {
16-
return analysisForString(element.toString(), indent);
5+
if (!element) {
6+
return [
7+
...analysisForTagName('code'),
8+
...analysisForTagName('pre'),
9+
...analysisForTagName('script'),
10+
...analysisForTagName('xmp')
11+
];
12+
} else {
13+
if (element) {
14+
if (element instanceof HTMLElement) {
15+
if (element.tagName === 'CODE' || element.tagName === 'PRE' || element.tagName === 'SCRIPT' || element.tagName === 'XMP') {
16+
return analysisForElement(element);
1717
}
18+
}
19+
return analysisForString(element + '', indent);
20+
} else {
21+
return null;
1822
}
23+
24+
}
1925
}
2026

2127
if (document.currentScript.hasAttribute('code')) {
22-
console.log(code())
28+
console.log(code())
2329
}
2430

2531
export default code;

0 commit comments

Comments
 (0)