-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmark-gfm-heading-generator
executable file
·237 lines (232 loc) · 15.6 KB
/
cmark-gfm-heading-generator
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env -S gawk -f
# SPDX-FileCopyrightText: 2024 Eli Array Minkoff
#
# SPDX-License-Identifier: 0BSD
# Intended to process output of cmark-gfm, and add anchor ids to headings
# to enable linking to them with the same "id" values GitHub itself uses.
# Tested on https://gist.github.com/eliminmax/01367a6a5e6ca01dc46c6dc012c3f4d0
# run this block on all lines starting with a header tag.
/^<h[1-6]>/{
# GitHub header ids are all lowercase.
head_id = tolower($0)
# remove HTML escaped elements like '&' - cmark-gfm normalizes them,
# so no need to worry about whether, for instance,
# '&48;' is a character to keep - it's '0', by the way, so it is.
gsub(/&[a-z]*;/, "", head_id)
# remove HTML tags
gsub(/<[^>]*>/, "", head_id)
# remove leading and trailing spaces
sub(/^ */, "", head_id)
sub(/ *$/, "", head_id)
# replace spaces with dashes
gsub(/ /, "-", head_id)
# remove any characters matched by regexps generated by my script found at
# https://gist.github.com/eliminmax/c61a3522431f5c675ba4bdd7fcdfc9a2
# and inspired by https://github.com/Flet/github-slugger/
gsub(/[\u1-\u23\u25-\u27\u2b-\u2c\u2f\u3a-\u3e\u40\u60]/, "", head_id)
gsub(/[\u7e-\ua9\uab-\ub4\ub6-\ub9\ubb-\ubf\ud7\uf7]/, "", head_id)
gsub(/[\u2c2-\u2c5\u2d2-\u2df\u2e5-\u2eb\u2ed\u2ef-\u2ff]/, "", head_id)
gsub(/[\u375\u378-\u379\u37e\u380-\u385\u387\u38b\u38d\u3a2]/, "", head_id)
gsub(/[\u3f6\u482\u530\u557-\u558\u55a-\u55f\u589-\u590]/, "", head_id)
gsub(/[\u5be\u5c0\u5c3\u5c6\u5c8-\u5cf\u5eb-\u5ee]/, "", head_id)
gsub(/[\u5f3-\u60f\u61b-\u61f\u66a-\u66d\u6d4\u6dd-\u6de]/, "", head_id)
gsub(/[\u6e9\u6fd-\u6fe\u700-\u70f\u74b-\u74c\u7b2-\u7bf]/, "", head_id)
gsub(/[\u7f6-\u7f9\u7fb-\u7fc\u7fe-\u7ff\u82e-\u83f]/, "", head_id)
gsub(/[\u85c-\u85f\u86b-\u86f\u888\u88f-\u897\u8e2]/, "", head_id)
gsub(/[\u964-\u965\u970\u984\u98d-\u98e\u991-\u992\u9a9]/, "", head_id)
gsub(/[\u9b1\u9b3-\u9b5\u9ba-\u9bb\u9c5-\u9c6\u9c9-\u9ca]/, "", head_id)
gsub(/[\u9cf-\u9d6\u9d8-\u9db\u9de\u9e4-\u9e5\u9f2-\u9fb]/, "", head_id)
gsub(/[\u9fd\u9ff-\ua00\ua04\ua0b-\ua0e\ua11-\ua12\ua29]/, "", head_id)
gsub(/[\ua31\ua34\ua37\ua3a-\ua3b\ua3d\ua43-\ua46]/, "", head_id)
gsub(/[\ua49-\ua4a\ua4e-\ua50\ua52-\ua58\ua5d\ua5f-\ua65]/, "", head_id)
gsub(/[\ua76-\ua80\ua84\ua8e\ua92\uaa9\uab1\uab4\uaba-\uabb]/, "", head_id)
gsub(/[\uac6\uaca\uace-\uacf\uad1-\uadf\uae4-\uae5]/, "", head_id)
gsub(/[\uaf0-\uaf8\ub00\ub04\ub0d-\ub0e\ub11-\ub12\ub29]/, "", head_id)
gsub(/[\ub31\ub34\ub3a-\ub3b\ub45-\ub46\ub49-\ub4a]/, "", head_id)
gsub(/[\ub4e-\ub54\ub58-\ub5b\ub5e\ub64-\ub65\ub70]/, "", head_id)
gsub(/[\ub72-\ub81\ub84\ub8b-\ub8d\ub91\ub96-\ub98\ub9b]/, "", head_id)
gsub(/[\ub9d\uba0-\uba2\uba5-\uba7\ubab-\ubad\ubba-\ubbd]/, "", head_id)
gsub(/[\ubc3-\ubc5\ubc9\ubce-\ubcf\ubd1-\ubd6\ubd8-\ube5]/, "", head_id)
gsub(/[\ubf0-\ubff\uc0d\uc11\uc29\uc3a-\uc3b\uc45\uc49]/, "", head_id)
gsub(/[\uc4e-\uc54\uc57\uc5b-\uc5c\uc5e-\uc5f\uc64-\uc65]/, "", head_id)
gsub(/[\uc70-\uc7f\uc84\uc8d\uc91\uca9\ucb4\ucba-\ucbb\ucc5]/, "", head_id)
gsub(/[\ucc9\ucce-\ucd4\ucd7-\ucdc\ucdf\uce4-\uce5\ucf0]/, "", head_id)
gsub(/[\ucf4-\ucff\ud0d\ud11\ud45\ud49\ud4f-\ud53]/, "", head_id)
gsub(/[\ud58-\ud5e\ud64-\ud65\ud70-\ud79\ud80\ud84]/, "", head_id)
gsub(/[\ud97-\ud99\udb2\udbc\udbe-\udbf\udc7-\udc9]/, "", head_id)
gsub(/[\udcb-\udce\udd5\udd7\ude0-\ude5\udf0-\udf1]/, "", head_id)
gsub(/[\udf4-\ue00\ue3b-\ue3f\ue4f\ue5a-\ue80\ue83\ue85]/, "", head_id)
gsub(/[\ue8b\uea4\uea6\uebe-\uebf\uec5\uec7\uecf\ueda-\uedb]/, "", head_id)
gsub(/[\uee0-\ueff\uf01-\uf17\uf1a-\uf1f\uf2a-\uf34\uf36]/, "", head_id)
gsub(/[\uf38\uf3a-\uf3d\uf48\uf6d-\uf70\uf85\uf98]/, "", head_id)
gsub(/[\ufbd-\ufc5\ufc7-\ufff\u104a-\u104f\u109e-\u109f]/, "", head_id)
gsub(/[\u10c6\u10c8-\u10cc\u10ce-\u10cf\u10fb\u1249]/, "", head_id)
gsub(/[\u124e-\u124f\u1257\u1259\u125e-\u125f\u1289]/, "", head_id)
gsub(/[\u128e-\u128f\u12b1\u12b6-\u12b7\u12bf\u12c1]/, "", head_id)
gsub(/[\u12c6-\u12c7\u12d7\u1311\u1316-\u1317\u135b-\u135c]/, "", head_id)
gsub(/[\u1360-\u137f\u1390-\u139f\u13f6-\u13f7\u13fe-\u1400]/, "", head_id)
gsub(/[\u166d-\u166e\u1680\u169b-\u169f\u16eb-\u16ed]/, "", head_id)
gsub(/[\u16f9-\u16ff\u1716-\u171e\u1735-\u173f\u1754-\u175f]/, "", head_id)
gsub(/[\u176d\u1771\u1774-\u177f\u17d4-\u17d6\u17d8-\u17db]/, "", head_id)
gsub(/[\u17de-\u17df\u17ea-\u180a\u180e\u181a-\u181f]/, "", head_id)
gsub(/[\u1879-\u187f\u18ab-\u18af\u18f6-\u18ff\u191f]/, "", head_id)
gsub(/[\u192c-\u192f\u193c-\u1945\u196e-\u196f\u1975-\u197f]/, "", head_id)
gsub(/[\u19ac-\u19af\u19ca-\u19cf\u19da-\u19ff\u1a1c-\u1a1f]/, "", head_id)
gsub(/[\u1a5f\u1a7d-\u1a7e\u1a8a-\u1a8f\u1a9a-\u1aa6]/, "", head_id)
gsub(/[\u1aa8-\u1aaf\u1acf-\u1aff\u1b4d-\u1b4f\u1b5a-\u1b6a]/, "", head_id)
gsub(/[\u1b74-\u1b7f\u1bf4-\u1bff\u1c38-\u1c3f\u1c4a-\u1c4c]/, "", head_id)
gsub(/[\u1c7e-\u1c7f\u1c89-\u1c8f\u1cbb-\u1cbc\u1cc0-\u1ccf]/, "", head_id)
gsub(/[\u1cd3\u1cfb-\u1cff\u1f16-\u1f17\u1f1e-\u1f1f]/, "", head_id)
gsub(/[\u1f46-\u1f47\u1f4e-\u1f4f\u1f58\u1f5a\u1f5c\u1f5e]/, "", head_id)
gsub(/[\u1f7e-\u1f7f\u1fb5\u1fbd\u1fbf-\u1fc1\u1fc5]/, "", head_id)
gsub(/[\u1fcd-\u1fcf\u1fd4-\u1fd5\u1fdc-\u1fdf\u1fed-\u1ff1]/, "", head_id)
gsub(/[\u1ff5\u1ffd-\u203e\u2041-\u2053\u2055-\u2070]/, "", head_id)
gsub(/[\u2072-\u207e\u2080-\u208f\u209d-\u20cf\u20f1-\u2101]/, "", head_id)
gsub(/[\u2103-\u2106\u2108-\u2109\u2114\u2116-\u2118]/, "", head_id)
gsub(/[\u211e-\u2123\u2125\u2127\u2129\u212e\u213a-\u213b]/, "", head_id)
gsub(/[\u2140-\u2144\u214a-\u214d\u214f-\u215f\u2189-\u2bff]/, "", head_id)
gsub(/[\u2ce5-\u2cea\u2cf4-\u2cff\u2d26\u2d28-\u2d2c]/, "", head_id)
gsub(/[\u2d2e-\u2d2f\u2d68-\u2d6e\u2d70-\u2d7e\u2d97-\u2d9f]/, "", head_id)
gsub(/[\u2da7\u2daf\u2db7\u2dbf\u2dc7\u2dcf\u2dd7\u2ddf]/, "", head_id)
gsub(/[\u2e00-\u2e2e\u2e30-\u3004\u3008-\u3020\u3030]/, "", head_id)
gsub(/[\u3036-\u3037\u303d-\u3040\u3097-\u3098\u309b-\u309c]/, "", head_id)
gsub(/[\u30a0\u30fb\u3100-\u3104\u3130\u318f-\u319f]/, "", head_id)
gsub(/[\u31c0-\u31ef\u3200-\u33ff\u4dc0-\u4dff\ua48d-\ua4cf]/, "", head_id)
gsub(/[\ua4fe-\ua4ff\ua60d-\ua60f\ua62c-\ua63f\ua673\ua67e]/, "", head_id)
gsub(/[\ua6f2-\ua716\ua720-\ua721\ua789-\ua78a\ua7cb-\ua7cf]/, "", head_id)
gsub(/[\ua7d2\ua7d4\ua7da-\ua7f1\ua828-\ua82b\ua82d-\ua83f]/, "", head_id)
gsub(/[\ua874-\ua87f\ua8c6-\ua8cf\ua8da-\ua8df\ua8f8-\ua8fa]/, "", head_id)
gsub(/[\ua8fc\ua92e-\ua92f\ua954-\ua95f\ua97d-\ua97f]/, "", head_id)
gsub(/[\ua9c1-\ua9ce\ua9da-\ua9df\ua9ff\uaa37-\uaa3f]/, "", head_id)
gsub(/[\uaa4e-\uaa4f\uaa5a-\uaa5f\uaa77-\uaa79\uaac3-\uaada]/, "", head_id)
gsub(/[\uaade-\uaadf\uaaf0-\uaaf1\uaaf7-\uab00\uab07-\uab08]/, "", head_id)
gsub(/[\uab0f-\uab10\uab17-\uab1f\uab27\uab2f\uab5b]/, "", head_id)
gsub(/[\uab6a-\uab6f\uabeb\uabee-\uabef\uabfa-\uabff]/, "", head_id)
gsub(/[\ud7a4-\ud7af\ud7c7-\ud7ca\ud7fc-\ud7ff\ue000-\uf8ff]/, "", head_id)
gsub(/[\ufa6e-\ufa6f\ufada-\ufaff\ufb07-\ufb12\ufb18-\ufb1c]/, "", head_id)
gsub(/[\ufb29\ufb37\ufb3d\ufb3f\ufb42\ufb45\ufbb2-\ufbd2]/, "", head_id)
gsub(/[\ufd3e-\ufd4f\ufd90-\ufd91\ufdc8-\ufdef\ufdfc-\ufdff]/, "", head_id)
gsub(/[\ufe10-\ufe1f\ufe30-\ufe32\ufe35-\ufe4c\ufe50-\ufe6f]/, "", head_id)
gsub(/[\ufe75\ufefd-\uff0f\uff1a-\uff20\uff3b-\uff3e\uff40]/, "", head_id)
gsub(/[\uff5b-\uff65\uffbf-\uffc1\uffc8-\uffc9\uffd0-\uffd1]/, "", head_id)
gsub(/[\uffd8-\uffd9\uffdd-\uffff\u1000c\u10027\u1003b]/, "", head_id)
gsub(/[\u1003e\u1004e-\u1004f\u1005e-\u1007f\u100fb-\u1013f]/, "", head_id)
gsub(/[\u10175-\u101fc\u101fe-\u1027f\u1029d-\u1029f]/, "", head_id)
gsub(/[\u102d1-\u102df\u102e1-\u102ff\u10320-\u1032c]/, "", head_id)
gsub(/[\u1034b-\u1034f\u1037b-\u1037f\u1039e-\u1039f]/, "", head_id)
gsub(/[\u103c4-\u103c7\u103d0\u103d6-\u103ff\u1049e-\u1049f]/, "", head_id)
gsub(/[\u104aa-\u104af\u104d4-\u104d7\u104fc-\u104ff]/, "", head_id)
gsub(/[\u10528-\u1052f\u10564-\u1056f\u1057b\u1058b\u10593]/, "", head_id)
gsub(/[\u10596\u105a2\u105b2\u105ba\u105bd-\u105ff]/, "", head_id)
gsub(/[\u10737-\u1073f\u10756-\u1075f\u10768-\u1077f\u10786]/, "", head_id)
gsub(/[\u107b1\u107bb-\u107ff\u10806-\u10807\u10809\u10836]/, "", head_id)
gsub(/[\u10839-\u1083b\u1083d-\u1083e\u10856-\u1085f]/, "", head_id)
gsub(/[\u10877-\u1087f\u1089f-\u108df\u108f3\u108f6-\u108ff]/, "", head_id)
gsub(/[\u10916-\u1091f\u1093a-\u1097f\u109b8-\u109bd]/, "", head_id)
gsub(/[\u109c0-\u109ff\u10a04\u10a07-\u10a0b\u10a14\u10a18]/, "", head_id)
gsub(/[\u10a36-\u10a37\u10a3b-\u10a3e\u10a40-\u10a5f]/, "", head_id)
gsub(/[\u10a7d-\u10a7f\u10a9d-\u10abf\u10ac8\u10ae7-\u10aff]/, "", head_id)
gsub(/[\u10b36-\u10b3f\u10b56-\u10b5f\u10b73-\u10b7f]/, "", head_id)
gsub(/[\u10b92-\u10bff\u10c49-\u10c7f\u10cb3-\u10cbf]/, "", head_id)
gsub(/[\u10cf3-\u10cff\u10d28-\u10d2f\u10d3a-\u10e7f\u10eaa]/, "", head_id)
gsub(/[\u10ead-\u10eaf\u10eb2-\u10efc\u10f1d-\u10f26]/, "", head_id)
gsub(/[\u10f28-\u10f2f\u10f51-\u10f6f\u10f86-\u10faf]/, "", head_id)
gsub(/[\u10fc5-\u10fdf\u10ff7-\u10fff\u11047-\u11065]/, "", head_id)
gsub(/[\u11076-\u1107e\u110bb-\u110c1\u110c3-\u110cf]/, "", head_id)
gsub(/[\u110e9-\u110ef\u110fa-\u110ff\u11135\u11140-\u11143]/, "", head_id)
gsub(/[\u11148-\u1114f\u11174-\u11175\u11177-\u1117f]/, "", head_id)
gsub(/[\u111c5-\u111c8\u111cd\u111db\u111dd-\u111ff\u11212]/, "", head_id)
gsub(/[\u11238-\u1123d\u11242-\u1127f\u11287\u11289\u1128e]/, "", head_id)
gsub(/[\u1129e\u112a9-\u112af\u112eb-\u112ef\u112fa-\u112ff]/, "", head_id)
gsub(/[\u11304\u1130d-\u1130e\u11311-\u11312\u11329\u11331]/, "", head_id)
gsub(/[\u11334\u1133a\u11345-\u11346\u11349-\u1134a]/, "", head_id)
gsub(/[\u1134e-\u1134f\u11351-\u11356\u11358-\u1135c]/, "", head_id)
gsub(/[\u11364-\u11365\u1136d-\u1136f\u11375-\u113ff]/, "", head_id)
gsub(/[\u1144b-\u1144f\u1145a-\u1145d\u11462-\u1147f\u114c6]/, "", head_id)
gsub(/[\u114c8-\u114cf\u114da-\u1157f\u115b6-\u115b7]/, "", head_id)
gsub(/[\u115c1-\u115d7\u115de-\u115ff\u11641-\u11643]/, "", head_id)
gsub(/[\u11645-\u1164f\u1165a-\u1167f\u116b9-\u116bf]/, "", head_id)
gsub(/[\u116ca-\u116ff\u1171b-\u1171c\u1172c-\u1172f]/, "", head_id)
gsub(/[\u1173a-\u1173f\u11747-\u117ff\u1183b-\u1189f]/, "", head_id)
gsub(/[\u118ea-\u118fe\u11907-\u11908\u1190a-\u1190b\u11914]/, "", head_id)
gsub(/[\u11917\u11936\u11939-\u1193a\u11944-\u1194f]/, "", head_id)
gsub(/[\u1195a-\u1199f\u119a8-\u119a9\u119d8-\u119d9\u119e2]/, "", head_id)
gsub(/[\u119e5-\u119ff\u11a3f-\u11a46\u11a48-\u11a4f]/, "", head_id)
gsub(/[\u11a9a-\u11a9c\u11a9e-\u11aaf\u11af9-\u11bff\u11c09]/, "", head_id)
gsub(/[\u11c37\u11c41-\u11c4f\u11c5a-\u11c71\u11c90-\u11c91]/, "", head_id)
gsub(/[\u11ca8\u11cb7-\u11cff\u11d07\u11d0a\u11d37-\u11d39]/, "", head_id)
gsub(/[\u11d3b\u11d3e\u11d48-\u11d4f\u11d5a-\u11d5f\u11d66]/, "", head_id)
gsub(/[\u11d69\u11d8f\u11d92\u11d99-\u11d9f\u11daa-\u11edf]/, "", head_id)
gsub(/[\u11ef7-\u11eff\u11f11\u11f3b-\u11f3d\u11f43-\u11f4f]/, "", head_id)
gsub(/[\u11f5a-\u11faf\u11fb1-\u11fff\u1239a-\u123ff]/, "", head_id)
gsub(/[\u1246f-\u1247f\u12544-\u12f8f\u12ff1-\u12fff]/, "", head_id)
gsub(/[\u13430-\u1343f\u13456-\u143ff\u14647-\u167ff]/, "", head_id)
gsub(/[\u16a39-\u16a3f\u16a5f\u16a6a-\u16a6f\u16abf]/, "", head_id)
gsub(/[\u16aca-\u16acf\u16aee-\u16aef\u16af5-\u16aff]/, "", head_id)
gsub(/[\u16b37-\u16b3f\u16b44-\u16b4f\u16b5a-\u16b62]/, "", head_id)
gsub(/[\u16b78-\u16b7c\u16b90-\u16e3f\u16e80-\u16eff]/, "", head_id)
gsub(/[\u16f4b-\u16f4e\u16f88-\u16f8e\u16fa0-\u16fdf\u16fe2]/, "", head_id)
gsub(/[\u16fe5-\u16fef\u16ff2-\u16fff\u187f8-\u187ff]/, "", head_id)
gsub(/[\u18cd6-\u18cff\u18d09-\u1afef\u1aff4\u1affc\u1afff]/, "", head_id)
gsub(/[\u1b123-\u1b131\u1b133-\u1b14f\u1b153-\u1b154]/, "", head_id)
gsub(/[\u1b156-\u1b163\u1b168-\u1b16f\u1b2fc-\u1bbff]/, "", head_id)
gsub(/[\u1bc6b-\u1bc6f\u1bc7d-\u1bc7f\u1bc89-\u1bc8f]/, "", head_id)
gsub(/[\u1bc9a-\u1bc9c\u1bc9f-\u1ceff\u1cf2e-\u1cf2f]/, "", head_id)
gsub(/[\u1cf47-\u1d164\u1d16a-\u1d16c\u1d173-\u1d17a]/, "", head_id)
gsub(/[\u1d183-\u1d184\u1d18c-\u1d1a9\u1d1ae-\u1d241]/, "", head_id)
gsub(/[\u1d245-\u1d3ff\u1d455\u1d49d\u1d4a0-\u1d4a1]/, "", head_id)
gsub(/[\u1d4a3-\u1d4a4\u1d4a7-\u1d4a8\u1d4ad\u1d4ba\u1d4bc]/, "", head_id)
gsub(/[\u1d4c4\u1d506\u1d50b-\u1d50c\u1d515\u1d51d\u1d53a]/, "", head_id)
gsub(/[\u1d53f\u1d545\u1d547-\u1d549\u1d551\u1d6a6-\u1d6a7]/, "", head_id)
gsub(/[\u1d6c1\u1d6db\u1d6fb\u1d715\u1d735\u1d74f\u1d76f]/, "", head_id)
gsub(/[\u1d789\u1d7a9\u1d7c3\u1d7cc-\u1d7cd\u1d800-\u1d9ff]/, "", head_id)
gsub(/[\u1da37-\u1da3a\u1da6d-\u1da74\u1da76-\u1da83]/, "", head_id)
gsub(/[\u1da85-\u1da9a\u1daa0\u1dab0-\u1deff\u1df1f-\u1df24]/, "", head_id)
gsub(/[\u1df2b-\u1dfff\u1e007\u1e019-\u1e01a\u1e022\u1e025]/, "", head_id)
gsub(/[\u1e02b-\u1e02f\u1e06e-\u1e08e\u1e090-\u1e0ff]/, "", head_id)
gsub(/[\u1e12d-\u1e12f\u1e13e-\u1e13f\u1e14a-\u1e14d]/, "", head_id)
gsub(/[\u1e14f-\u1e28f\u1e2af-\u1e2bf\u1e2fa-\u1e4cf]/, "", head_id)
gsub(/[\u1e4fa-\u1e7df\u1e7e7\u1e7ec\u1e7ef\u1e7ff]/, "", head_id)
gsub(/[\u1e8c5-\u1e8cf\u1e8d7-\u1e8ff\u1e94c-\u1e94f]/, "", head_id)
gsub(/[\u1e95a-\u1edff\u1ee04\u1ee20\u1ee23\u1ee25-\u1ee26]/, "", head_id)
gsub(/[\u1ee28\u1ee33\u1ee38\u1ee3a\u1ee3c-\u1ee41]/, "", head_id)
gsub(/[\u1ee43-\u1ee46\u1ee48\u1ee4a\u1ee4c\u1ee50\u1ee53]/, "", head_id)
gsub(/[\u1ee55-\u1ee56\u1ee58\u1ee5a\u1ee5c\u1ee5e\u1ee60]/, "", head_id)
gsub(/[\u1ee63\u1ee65-\u1ee66\u1ee6b\u1ee73\u1ee78\u1ee7d]/, "", head_id)
gsub(/[\u1ee7f\u1ee8a\u1ee9c-\u1eea0\u1eea4\u1eeaa]/, "", head_id)
gsub(/[\u1eebc-\u1fbef\u1fbfa-\u1ffff\u2a6e0-\u2a6ff]/, "", head_id)
gsub(/[\u2b73a-\u2b73f\u2b81e-\u2b81f\u2cea2-\u2ceaf]/, "", head_id)
gsub(/[\u2ebe1-\u2f7ff\u2fa1e-\u2ffff\u3134b-\u3134f]/, "", head_id)
gsub(/[\u323b0-\ue00ff\ue01f0-\u10ffff]/, "", head_id)
gsub(/[[\]\{\}\\\^\$\|\*\?\(\)\.]/, "", head_id)
# check for previous headings that would have a name collision
if (head_ids[head_id] > 0) {
# generate the new id by appending "-N", where N is the number of
# previous headings with the same name - except in some weird edge
# cases that I tried to account for.
new_id = sprintf("%s-%d", head_id, head_ids[head_id])
# check for a bizarre edge case where a previous heading ending in
# a number would have a name collision with this one
# like a heading called 'foo 1' followed by 2 headings called 'foo'
# handle it in a way compatible with what GitHub does
# keep incrementing head_ids[head_id] until there's no collision
while (head_ids[new_id] > 0) {
new_id = sprintf("%s-%d", head_id, ++head_ids[head_id])
}
# we have our new id
head_id = new_id
# handling another edge case that's kind of the reverse of the last one
# as an example, if there are 2 headings called 'foo' followed by one
# called 'foo 1', then one more 'foo', GitHub's ids for them are 'foo',
# 'foo-1', 'foo-1-1', and 'foo-2' respectively.
# dealing with that is as simple as adding the new id to the known ids
head_ids[new_id]++
} else {
# if there is no collision with a previous id, simply increment it
head_ids[head_id]++
}
# insert the new id into the header tag
sub(/>/, " id=\""head_id"\">")
}
# print all lines
{print}