forked from Effect-TS/effect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code2jsdoc-example.html
67 lines (65 loc) · 1.87 KB
/
code2jsdoc-example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>code 2 jsdoc @example</title>
<style>
body {
font-family: Arial;
}
textarea {
font-family: Courier, monospace;
border: 1px solid #ccc;
border-radius: 4px;
margin: 0.5rem;
padding: 0.5rem;
font-size: medium;
}
</style>
</head>
<body>
<center>
<h1>code 2 jsdoc @example</h1>
<p>
Given an example pasted in the left textarea, will generate a result
ready to be pasted into a JSDoc comment.
</p>
<div id="app">
<textarea
id="code"
cols="80"
rows="40"
onkeyup="document.getElementById('comment').value = t(this.value)"
></textarea>
<textarea id="comment" cols="80" rows="40" style="color: green">
</textarea>
</div>
</center>
<script>
const code = document.getElementById("code")
const comment = document.getElementById("comment")
function t(src) {
// Replace multiline comments with lines prefixed by '// ' and preserve indentation
const withoutMultilineComments = src.replace(
/\/\*([\s\S]*?)\*\//g,
(_, content) => {
const commentLines = content
.split("\n")
.filter((line) => line.trim() !== "")
return commentLines
.map((line) => {
const trimmedLine = line.replace(/^\s*\*/, "").trim()
const indent = line.match(/^\s*/)[0]
return `// ${indent}${trimmedLine}`
})
.join("\n")
}
)
const lines = withoutMultilineComments
.split("\n")
.map((line) => "* " + line)
return "*\n* @example\n* ```ts\n" + lines.join("\n") + "\n* ```\n*\n"
}
</script>
</body>
</html>