use-highlighted-code.ts 570 B

123456789101112131415161718192021
  1. import { useEffect, useState } from 'react'
  2. import { highlightCode } from './index'
  3. export function useHighlightedCode(code: string, language: string | null | undefined, themeName: string) {
  4. const [tokens, setTokens] = useState<any>(null)
  5. useEffect(() => {
  6. let active = true
  7. setTokens(null)
  8. void highlightCode(code, language, themeName).then((nextTokens) => {
  9. if (active) setTokens(nextTokens)
  10. })
  11. return () => {
  12. active = false
  13. }
  14. }, [code, language, themeName])
  15. return tokens
  16. }