|
| 1 | +// Inspired by https://github.com/rexxars/react-refractor |
| 2 | +import React from 'react'; |
| 3 | +import refractor from 'refractor/core'; |
| 4 | +import js from 'refractor/lang/javascript'; |
| 5 | +import jsx from 'refractor/lang/jsx'; |
| 6 | +import bash from 'refractor/lang/bash'; |
| 7 | +import css from 'refractor/lang/css'; |
| 8 | +import diff from 'refractor/lang/diff'; |
| 9 | +import hastToHtml from 'hast-util-to-html'; |
| 10 | +import rangeParser from 'parse-numeric-range'; |
| 11 | +import highlightLine from '@lib/rehype-highlight-line'; |
| 12 | +import highlightWord from '@lib/rehype-highlight-word'; |
| 13 | +import { Pre } from './Pre'; |
| 14 | + |
| 15 | +refractor.register(js); |
| 16 | +refractor.register(jsx); |
| 17 | +refractor.register(bash); |
| 18 | +refractor.register(css); |
| 19 | +refractor.register(diff); |
| 20 | + |
| 21 | +type PreProps = Omit<React.ComponentProps<typeof Pre>, 'css'>; |
| 22 | + |
| 23 | +type CodeBlockProps = PreProps & { |
| 24 | + language: 'js' | 'jsx' | 'bash' | 'css' | 'diff'; |
| 25 | + value: string; |
| 26 | + line?: string; |
| 27 | + css?: any; |
| 28 | + mode?: 'static' | 'typewriter' | 'interactive'; |
| 29 | + showLineNumbers?: boolean; |
| 30 | +}; |
| 31 | + |
| 32 | +export const CodeBlock = React.forwardRef<HTMLPreElement, CodeBlockProps>( |
| 33 | + (_props, forwardedRef) => { |
| 34 | + const { |
| 35 | + language, |
| 36 | + value, |
| 37 | + line = '0', |
| 38 | + className = '', |
| 39 | + mode, |
| 40 | + css, |
| 41 | + variant, |
| 42 | + showLineNumbers, |
| 43 | + ...props |
| 44 | + } = _props; |
| 45 | + let result = refractor.highlight(value, language); |
| 46 | + |
| 47 | + result = highlightLine(result, rangeParser(line)); |
| 48 | + |
| 49 | + result = highlightWord(result); |
| 50 | + |
| 51 | + // convert to html |
| 52 | + result = hastToHtml(result); |
| 53 | + |
| 54 | + // TODO reset theme |
| 55 | + |
| 56 | + const classes = `language-${language} ${className}`; |
| 57 | + |
| 58 | + if (mode === 'typewriter') { |
| 59 | + return ( |
| 60 | + <CodeTypewriter className={classes} css={css} variant={variant} value={result} {...props} /> |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + return ( |
| 65 | + <Pre |
| 66 | + ref={forwardedRef} |
| 67 | + className={classes} |
| 68 | + css={css} |
| 69 | + variant={variant} |
| 70 | + data-line-numbers={showLineNumbers} |
| 71 | + {...props} |
| 72 | + > |
| 73 | + <code className={classes} dangerouslySetInnerHTML={{ __html: result }} /> |
| 74 | + </Pre> |
| 75 | + ); |
| 76 | + } |
| 77 | +); |
| 78 | + |
| 79 | +/** |
| 80 | + * recursively get all text nodes as an array for a given element |
| 81 | + */ |
| 82 | +function getTextNodes(node) { |
| 83 | + let childTextNodes = []; |
| 84 | + |
| 85 | + if (!node.hasChildNodes()) return; |
| 86 | + |
| 87 | + const childNodes = node.childNodes; |
| 88 | + for (let i = 0; i < childNodes.length; i++) { |
| 89 | + if (childNodes[i].nodeType == Node.TEXT_NODE) { |
| 90 | + childTextNodes.push(childNodes[i]); |
| 91 | + } else if (childNodes[i].nodeType == Node.ELEMENT_NODE) { |
| 92 | + Array.prototype.push.apply(childTextNodes, getTextNodes(childNodes[i])); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return childTextNodes; |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * given a text node, wrap each character in the |
| 101 | + * given tag. |
| 102 | + */ |
| 103 | +function wrapEachCharacter(textNode, tag, count) { |
| 104 | + const text = textNode.nodeValue; |
| 105 | + const parent = textNode.parentNode; |
| 106 | + |
| 107 | + const characters = text.split(''); |
| 108 | + characters.forEach(function (character, letterIndex) { |
| 109 | + const delay = (count + letterIndex) * 50; |
| 110 | + var element = document.createElement(tag); |
| 111 | + var characterNode = document.createTextNode(character); |
| 112 | + element.appendChild(characterNode); |
| 113 | + element.style.opacity = 0; |
| 114 | + element.style.transition = `all ease 0ms ${delay}ms`; |
| 115 | + |
| 116 | + parent.insertBefore(element, textNode); |
| 117 | + |
| 118 | + // skip a couple of frames to trigger transition |
| 119 | + requestAnimationFrame(() => requestAnimationFrame(() => (element.style.opacity = 1))); |
| 120 | + }); |
| 121 | + |
| 122 | + parent.removeChild(textNode); |
| 123 | +} |
| 124 | + |
| 125 | +function CodeTypewriter({ value, className, css, variant, ...props }) { |
| 126 | + const wrapperRef = React.useRef(null); |
| 127 | + |
| 128 | + React.useEffect(() => { |
| 129 | + const wrapper = wrapperRef.current; |
| 130 | + |
| 131 | + if (wrapper) { |
| 132 | + var allTextNodes = getTextNodes(wrapper); |
| 133 | + |
| 134 | + let count = 0; |
| 135 | + allTextNodes.forEach((textNode) => { |
| 136 | + wrapEachCharacter(textNode, 'span', count); |
| 137 | + count = count + textNode.nodeValue.length; |
| 138 | + }); |
| 139 | + wrapper.style.opacity = 1; |
| 140 | + } |
| 141 | + |
| 142 | + return () => (wrapper.innerHTML = value); |
| 143 | + }, []); |
| 144 | + |
| 145 | + return ( |
| 146 | + <Pre className={className} css={css} variant={variant} {...props}> |
| 147 | + <code |
| 148 | + ref={wrapperRef} |
| 149 | + style={{ opacity: 0 }} |
| 150 | + className={className} |
| 151 | + dangerouslySetInnerHTML={{ __html: value }} |
| 152 | + /> |
| 153 | + </Pre> |
| 154 | + ); |
| 155 | +} |
0 commit comments