Skip to content

Commit 43100d1

Browse files
authored
1 parent 0fbf511 commit 43100d1

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Prompts used are linked to from [the commit messages](https://github.com/simonw/
2424
- [Timezones](https://tools.simonwillison.net/timezones) - select two timezones to see a table comparing their times for the next 48 hours
2525
- [Social media cropper](https://tools.simonwillison.net/social-media-cropper) - open or paste in an image, crop it to 2x1 and download a compressed JPEG for use as a social media card
2626
- [Writing Style Analyzer](https://tools.simonwillison.net/writing-style) - identify weasel words, passive voice, duplicate words - adapted from [these shell scripts](https://matt.might.net/articles/shell-scripts-for-passive-voice-weasel-words-duplicates/) published by Matt Might
27+
- [Navigation for headings](https://tools.simonwillison.net/nav-for-headings) - paste in an HTML document with headings, each heading is assigned a unique ID and the tool then generates a navigation `<ul>`
2728

2829
## LLM playgrounds and debuggers
2930

nav-for-headings.html

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>HTML Header Processor</title>
7+
<style>
8+
* {
9+
box-sizing: border-box;
10+
}
11+
12+
body {
13+
font-family: Helvetica, Arial, sans-serif;
14+
line-height: 1.4;
15+
max-width: 1200px;
16+
margin: 0 auto;
17+
padding: 20px;
18+
}
19+
20+
.input-group {
21+
margin-bottom: 20px;
22+
}
23+
24+
label {
25+
display: block;
26+
margin-bottom: 8px;
27+
font-weight: bold;
28+
}
29+
30+
input, textarea {
31+
width: 100%;
32+
font-size: 16px;
33+
padding: 8px;
34+
border: 1px solid #ccc;
35+
border-radius: 4px;
36+
}
37+
38+
textarea {
39+
min-height: 200px;
40+
font-family: monospace;
41+
}
42+
43+
button {
44+
background: #0066cc;
45+
color: white;
46+
border: none;
47+
padding: 10px 20px;
48+
border-radius: 4px;
49+
cursor: pointer;
50+
font-size: 16px;
51+
}
52+
53+
button:hover {
54+
background: #0052a3;
55+
}
56+
</style>
57+
</head>
58+
<body>
59+
<div class="input-group">
60+
<label for="urlInput">Page URL:</label>
61+
<input type="url" id="urlInput" placeholder="https://example.com/page">
62+
</div>
63+
64+
<div class="input-group">
65+
<label for="htmlInput">Input HTML:</label>
66+
<textarea id="htmlInput" placeholder="Paste HTML here..."></textarea>
67+
</div>
68+
69+
<div class="input-group">
70+
<button onclick="processHTML()">Process Headers</button>
71+
</div>
72+
73+
<div class="input-group">
74+
<label for="processedHtml">Processed HTML with IDs:</label>
75+
<textarea id="processedHtml" readonly></textarea>
76+
</div>
77+
78+
<div class="input-group">
79+
<label for="headerLinks">Header Links:</label>
80+
<textarea id="headerLinks" readonly></textarea>
81+
</div>
82+
83+
<script type="module">
84+
function createSlug(text) {
85+
return text
86+
.toLowerCase()
87+
.replace(/[^a-z0-9\s-]/g, '')
88+
.replace(/\s+/g, '-')
89+
.replace(/-+/g, '-')
90+
.trim()
91+
}
92+
93+
function generateUniqueId(text, existingIds) {
94+
let baseId = createSlug(text)
95+
let id = baseId
96+
let counter = 1
97+
98+
while (existingIds.has(id)) {
99+
id = `${baseId}-${counter}`
100+
counter++
101+
}
102+
103+
existingIds.add(id)
104+
return id
105+
}
106+
107+
window.processHTML = function() {
108+
const html = document.getElementById('htmlInput').value
109+
const url = document.getElementById('urlInput').value
110+
const parser = new DOMParser()
111+
const doc = parser.parseFromString(html, 'text/html')
112+
const existingIds = new Set()
113+
const headers = doc.querySelectorAll('h1, h2, h3, h4, h5, h6')
114+
const headerLinks = []
115+
116+
headers.forEach(header => {
117+
if (!header.id) {
118+
header.id = generateUniqueId(header.textContent, existingIds)
119+
}
120+
headerLinks.push(` <li><a href="${url}#${header.id}">${header.textContent}</a></li>`)
121+
})
122+
123+
document.getElementById('processedHtml').value = doc.body.innerHTML
124+
document.getElementById('headerLinks').value = '<ul>\n' + headerLinks.join('\n') + '\n</ul>'
125+
}
126+
</script>
127+
</body>
128+
</html>

0 commit comments

Comments
 (0)