Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move citations component to separate lib, no longer needed for this project but ... #65

Open
github-actions bot opened this issue Sep 6, 2022 · 0 comments
Labels

Comments

@github-actions
Copy link

github-actions bot commented Sep 6, 2022

Move citations component to separate lib, no longer needed for this project but a waste to just throw away

* TODO: Move citations component to separate lib, no longer needed for this project but a waste to just throw away

/* eslint-disable @typescript-eslint/no-shadow */
/* eslint-disable react/no-unstable-nested-components */
import React, { ReactNode, useMemo } from 'react'

// @ts-expect-error citation???
import Citation from 'citation-js'
import { unified } from 'unified'
import rehype2react, { Options } from 'rehype-react'
import rehypeParse from 'rehype-parse'
import {
  Box,
  ListItem,
  Text,
  OrderedList,
  UnorderedList,
  Link,
  Heading,
  VStack,
} from '@chakra-ui/react'
import { ExternalLinkIcon } from '@chakra-ui/icons'
import { CSLCitation } from '@zkp/types'
// import { noteStyle } from '../NoteStyle'

interface CitationProps {
  csl: CSLCitation[]
}

/**
 * TODO: Move citations component to separate lib, no longer needed for this project but a waste to just throw away
 */
export const Citations = (props: CitationProps) => {
  const { csl } = props

  const bibliography = useMemo(() => {
    const cite = new Citation(csl)
    return cite.format('bibliography', { format: 'html', style: 'apa' })
  }, [csl])

  const citations = useMemo(() => {
    const htmlWithLinks = bibliography.replace(/(https?:\/\/([^<]*))/g, '<a href="$1">$1</a>')

    const processor = unified()
      .use(rehypeParse)
      .use(rehype2react, {
        createElement: React.createElement,
        Fragment: React.Fragment,
        components: {
          html: ({ children }) => children as React.ReactElement,
          body: ({ children }) => children as React.ReactElement,
          head: () => null,
          a: ({ href, children }) => (
            <Link color="primary" style={{ alignItems: 'center' }} href={href as string} isExternal>
              {children as string}
              <ExternalLinkIcon mb="2px" ml="2px" />
            </Link>
          ),
          p: Text,
          div: ({ className, children, ...props }) => {
            if ((className as string)?.includes('csl-bib-body')) return children as ReactNode
            if (!(className as string)?.includes('csl-entry')) {
              return (
                <Box {...{ className: className as string, ...props }}>
                  {children as React.ReactNode}
                </Box>
              )
            }
            return (
              <Text variant="org" {...props}>
                {children as React.ReactNode}
              </Text>
            )
          },
          li: ListItem,
          ol: OrderedList,
          ul: UnorderedList,
          h: Heading,
          em: ({ children, ...props }) => (
            // @ts-expect-error idk man, something about em not being assignable to p
            <Text as="em" variant="org" {...props}>
              {children as React.ReactNode}
            </Text>
          ),
        },
      } as Options)

    return processor.processSync(htmlWithLinks).result as React.ReactNode
  }, [bibliography])

  return (
    <>
      <Heading id="references" style={{ scrollMarginTop: '100px' }} size="md">
        References
      </Heading>
      {/* <VStack sx={{ ...noteStyle }} alignItems="flex-start">
        {citations}
      </VStack> */}
    </>
  )
}

03181909fffff08097c751b5de599740c31be7c6

@github-actions github-actions bot added the todo label Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants