Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

wrap-code-terms.js 1.3 KB

You have to be logged in to leave a comment. Sign In
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
  1. import escape from 'lodash/escape'
  2. const wordsLongerThan18Chars = /[\S]{18,}/g
  3. const camelCaseChars = /([a-z])([A-Z])/g
  4. const underscoresAfter12thChar = /([\w:]{12}[^_]*?)_/g
  5. const slashChars = /([/\\])/g
  6. // This module improves table rendering on reference pages by inserting a <wbr>
  7. // tag in code terms that use camelcase, slashes, or underscores, inspired by
  8. // http://heap.ch/blog/2016/01/19/camelwrap/
  9. export default function () {
  10. const codeTerms = document.querySelectorAll('#article-contents table code')
  11. if (!codeTerms) return
  12. codeTerms.forEach(node => {
  13. // Do the wrapping on the inner text only, so we don't modify hrefs
  14. const oldText = escape(node.textContent)
  15. const newText = oldText.replace(wordsLongerThan18Chars, (str) => {
  16. return str
  17. // GraphQL code terms use camelcase
  18. .replace(camelCaseChars, '$1<wbr>$2')
  19. // REST code terms use underscores
  20. // to keep word breaks looking nice, only break on underscores after the 12th char
  21. // so `has_organization_projects` will break after `has_organization` instead of after `has_`
  22. .replace(underscoresAfter12thChar, '$1_<wbr>')
  23. // Some Actions reference pages have tables with code terms separated by slashes
  24. .replace(slashChars, '$1<wbr>')
  25. })
  26. node.innerHTML = node.innerHTML.replace(oldText, newText)
  27. })
  28. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...