-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
nav-for-headings.html
129 lines (111 loc) · 2.88 KB
/
nav-for-headings.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Header Processor</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Helvetica, Arial, sans-serif;
line-height: 1.4;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.input-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}
input, textarea {
width: 100%;
font-size: 16px;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}
textarea {
min-height: 200px;
font-family: monospace;
}
button {
background: #0066cc;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background: #0052a3;
}
</style>
</head>
<body>
<div class="input-group">
<label for="urlInput">Page URL:</label>
<input type="url" id="urlInput" placeholder="https://example.com/page">
</div>
<div class="input-group">
<label for="htmlInput">Input HTML:</label>
<textarea id="htmlInput" placeholder="Paste HTML here..."></textarea>
</div>
<div class="input-group">
<button onclick="processHTML()">Process Headers</button>
</div>
<div class="input-group">
<label for="processedHtml">Processed HTML with IDs:</label>
<textarea id="processedHtml" readonly></textarea>
</div>
<div class="input-group">
<label for="headerLinks">Header Links:</label>
<textarea id="headerLinks" readonly></textarea>
</div>
<script type="module">
function createSlug(text) {
return text
.toLowerCase()
.replace(/[^a-z0-9\s-]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-')
.trim()
}
function generateUniqueId(text, existingIds) {
let baseId = createSlug(text)
let id = baseId
let counter = 1
while (existingIds.has(id)) {
id = `${baseId}-${counter}`
counter++
}
existingIds.add(id)
return id
}
window.processHTML = function() {
const html = document.getElementById('htmlInput').value
const url = document.getElementById('urlInput').value
const parser = new DOMParser()
const doc = parser.parseFromString(html, 'text/html')
// Collect all existing IDs on the page first
const existingIds = new Set([...doc.querySelectorAll('[id]')].map(el => el.id))
const headers = doc.querySelectorAll('h1, h2, h3, h4, h5, h6')
const headerLinks = []
headers.forEach(header => {
if (!header.id) {
header.id = generateUniqueId(header.textContent, existingIds)
}
headerLinks.push(` <li><a href="${url}#${header.id}">${header.textContent}</a></li>`)
})
document.getElementById('processedHtml').value = doc.body.innerHTML
document.getElementById('headerLinks').value = '<ul>\n' + headerLinks.join('\n') + '\n</ul>'
}
</script>
</body>
</html>