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.ts 1.5 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
33
34
35
36
37
38
  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. // Return early if a child node is an anchor element
  14. const hasChildAnchor = Array.from(node.childNodes).some((child) => child.nodeName === 'A')
  15. if (hasChildAnchor) return
  16. // Do the wrapping on the inner text only
  17. const oldText = escape(node.textContent || '')
  18. const newText = oldText.replace(wordsLongerThan18Chars, (str) => {
  19. return (
  20. str
  21. // GraphQL code terms use camelcase
  22. .replace(camelCaseChars, '$1<wbr>$2')
  23. // REST code terms use underscores
  24. // to keep word breaks looking nice, only break on underscores after the 12th char
  25. // so `has_organization_projects` will break after `has_organization` instead of after `has_`
  26. .replace(underscoresAfter12thChar, '$1_<wbr>')
  27. // Some Actions reference pages have tables with code terms separated by slashes
  28. .replace(slashChars, '$1<wbr>')
  29. )
  30. })
  31. node.innerHTML = node.innerHTML.replace(oldText, newText)
  32. })
  33. }
Tip!

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

Comments

Loading...