Skip to content

Commit 3fd00ec

Browse files
committed
Initial commit
0 parents  commit 3fd00ec

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed

index.html

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Markdown ToC Generator</title>
6+
7+
<style>
8+
body {
9+
padding: 0;
10+
margin: 0;
11+
font-family: sans-serif;
12+
background-color: #3b3b3b;
13+
color: white;
14+
}
15+
a, a:hover, a:active, a:visited {
16+
color: #8cd4a8;
17+
text-decoration: none;
18+
margin: 0 5px;
19+
}
20+
.container {
21+
display: grid;
22+
grid-template-columns: 50% 50%;
23+
grid-template-rows: 50px 50px auto 10px;
24+
grid-gap: 10px;
25+
padding: 10px;
26+
width: calc(100vw - 3 * 10px);
27+
height: calc(100vh - 2 * 10px);
28+
}
29+
.header {
30+
display: flex;
31+
justify-content: center;
32+
align-items: flex-end;
33+
font-size: 22px;
34+
}
35+
.area {
36+
resize: none;
37+
background-color: #5b5b5b;
38+
color: #c8c8c8;
39+
border: 1px solid #4e4e4e;
40+
padding: 10px;
41+
font-size: 10px;
42+
font-family: monospace;
43+
}
44+
#title {
45+
grid-column: 1 / 3;
46+
display: flex;
47+
justify-content: flex-start;
48+
align-items: center;
49+
font-size: 32px;
50+
}
51+
#input-header {
52+
grid-column: 1 / 2;
53+
grid-row: 2 / 3;
54+
}
55+
#output-header {
56+
grid-column: 2 / 3;
57+
grid-row: 2 / 3;
58+
}
59+
#input-area {
60+
grid-column: 1 / 2;
61+
grid-row: 3 / 4;
62+
}
63+
#output-area {
64+
grid-column: 2 / 3;
65+
grid-row: 3 / 4;
66+
}
67+
.footer {
68+
grid-column: 1 / 3;
69+
grid-row: 4 / 5;
70+
font-size: 12px;
71+
display: flex;
72+
justify-content: flex-end;
73+
align-items: center;
74+
}
75+
</style>
76+
</head>
77+
<body>
78+
79+
<div class="container">
80+
<div id="title">Online Markdown Table of Contents Generator</div>
81+
<div id="input-header" class="header">Paste your Markdown text here</div>
82+
<div id="output-header" class="header">Output</div>
83+
<textarea name="input-area" class="area" id="input-area">
84+
# Foo
85+
86+
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
87+
88+
## Bar
89+
90+
Nulla facilisi. Proin venenatis urna at justo tempus, laoreet rutrum nisi efficitur.
91+
92+
Etiam iaculis lobortis nibh, et accumsan diam imperdiet vitae.
93+
94+
## Foobar
95+
96+
Fusce at mollis justo. Suspendisse eleifend neque diam, et ullamcorper urna gravida vitae.
97+
98+
# Barfoo
99+
100+
Donec ultricies egestas augue non dignissim.
101+
102+
## Barbar
103+
104+
Praesent fringilla pulvinar pellentesque. Ut elementum ultrices tortor, ut hendrerit diam tempor vitae.
105+
</textarea>
106+
<textarea name="output-area" class="area" id="output-area" readonly></textarea>
107+
<div class="footer"><a href="//luciopaiva.com" target="_blank">Lucio Paiva</a> 2018</div>
108+
</div>
109+
110+
<script src="index.js"></script>
111+
<script>new MarkdownToc();</script>
112+
113+
</body>
114+
</html>

index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
class MarkdownToc {
3+
4+
constructor () {
5+
this.inputArea = document.getElementById("input-area");
6+
this.outputArea = document.getElementById("output-area");
7+
8+
this.inputArea.addEventListener("input", this.process.bind(this));
9+
this.process();
10+
}
11+
12+
process() {
13+
const input = this.inputArea.value;
14+
const menus = ["# Table of contents", ""];
15+
let isCodeBlock = false;
16+
17+
for (let line of input.split("\n")) {
18+
19+
if (line.startsWith("```")) {
20+
isCodeBlock = !isCodeBlock;
21+
}
22+
23+
if (isCodeBlock) {
24+
continue;
25+
}
26+
27+
if (line.startsWith("#")) {
28+
const match = line.match(/(#+)\s*(.*)/);
29+
const level = match[1].length - 1;
30+
const title = match[2];
31+
const link = title.toLocaleLowerCase()
32+
.replace(/\s/g, "-")
33+
.replace(/[^A-Za-z0-9-]/g, "");
34+
const menu = `${" ".repeat(level)}- [${title}](#${link})`;
35+
menus.push(menu);
36+
}
37+
}
38+
39+
this.outputArea.value = menus.join("\n");
40+
}
41+
}

0 commit comments

Comments
 (0)