Skip to content

Commit

Permalink
Move utils back into main file
Browse files Browse the repository at this point in the history
  • Loading branch information
KittyGiraudel committed Mar 23, 2021
1 parent d97fa70 commit 150e07e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
27 changes: 26 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react'
import PropTypes from 'prop-types'
import { getIdFromTree } from './utils'

const FootnotesContext = React.createContext({})

Expand Down Expand Up @@ -160,3 +159,29 @@ export const FootnotesProvider = ({ children, footnotesTitleId }) => {
FootnotesProvider.defaultProps = {
footnotesTitleId: 'footnotes-label',
}

function getTextFromTree(tree) {
let text = ''

if (typeof tree === 'string') {
text += tree
} else if (Array.isArray(tree)) {
text += tree.map(getTextFromTree).join('')
} else if (tree.props.children) {
text += getTextFromTree(tree.props.children)
}

return text
}

export function getIdFromTree(tree) {
return (
getTextFromTree(tree)
.toLowerCase()
// Remove any character that is not a letter, a number, an hyphen or an
// underscore, regardless of casing
.replace(/[^a-z0-9-_\s]/g, '')
// Replace all spaces with hyphens
.replace(/\s+/g, '-')
)
}
3 changes: 1 addition & 2 deletions src/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react'
import { Footnotes, FootnotesProvider, FootnoteRef } from './'
import { getIdFromTree } from './utils'
import { Footnotes, FootnotesProvider, FootnoteRef, getIdFromTree } from './'
import { render, configure, screen } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect'

Expand Down
22 changes: 0 additions & 22 deletions src/utils.js

This file was deleted.

0 comments on commit 150e07e

Please sign in to comment.