egoist / docute

📚 Effortless documentation, done right.
https://docute.egoist.dev
MIT License
3.8k stars 427 forks source link

querySelector fails for auto generated ids from sub sections headings that start with a number #258

Open nborko opened 5 years ago

nborko commented 5 years ago

If a section heading starts with a number, e.g.

## 1. Section 1

The auto-generated ID of #1-section-1 is not a valid querySelector and throws an error when selected in the sidebar (in src/Home.vue#jumpToHash). So it needs to be quoted, e.g.

const el = document.querySelector(hash.replace(/^#\d/, m => '#\\' + m.charCodeAt(1).toString(16) + ' '))

(NOTE: CSS.escape will cover more cases, but the MDN compatibility matrix shows some notable holes, e.g. Safari on IOS, and the generated hash avoid most other issues anyway as far as I can tell)

Alternatively, a more robust hash could be generated in this situation. Or as a workaround, just never start a section heading with a number :-)