Skip to content

Commit

Permalink
Merge pull request #390 from primer/fix_rails_react_octicons
Browse files Browse the repository at this point in the history
Updating storiesFromMarkdown to read in rails octicons helper and replace with react component
  • Loading branch information
jonrohan authored Nov 6, 2017
2 parents e314b6f + 49e98ce commit c584ebe
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion .storybook/lib/storiesFromMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ import select from 'unist-util-select'
import findBefore from 'unist-util-find-before'
import htmlToReact from 'html-to-react'
import parsePairs from 'parse-pairs'
import React from 'react'
import ReactDOMServer from 'react-dom/server'
import {Octicon} from '../Octicon'

const htmlParser = new htmlToReact.Parser()

const railsOcticonToReact = (html) => {
// <%= octicon "tools" %> to <Octicon name="tools" />
const octre = /<%= octicon ["']([a-z\-]+)["'][^%]*%>/gi
html = html.replace(octre, (match, name) => {
return ReactDOMServer.renderToStaticMarkup(<Octicon name={name} />)
})
return html
}

const nodeToStory = (node, file) => {
const html = node.value
const html = railsOcticonToReact(node.value)
const element = htmlParser.parse(html)
const pairs = node.lang.replace(/^html\s*/, '')
const attrs = pairs.length ? parsePairs(pairs) : {}
Expand Down

0 comments on commit c584ebe

Please sign in to comment.