Skip to content

Commit

Permalink
chore: Format with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Jun 24, 2019
1 parent 6728d13 commit 182f34c
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 51 deletions.
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": false,
"printWidth": 100,
"bracketSpacing": false,
"singleQuote": true
}
46 changes: 29 additions & 17 deletions src/ast-to-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ function astToReact(node, options, parent = {}, index = 0) {
const pos = node.position.start
const key = [node.type, pos.line, pos.column].join('-')

if (typeof renderer !== 'function' && typeof renderer !== 'string' && !isReactFragment(renderer)) {
if (
typeof renderer !== 'function' &&
typeof renderer !== 'string' &&
!isReactFragment(renderer)
) {
throw new Error(`Renderer for type \`${node.type}\` not defined or is not renderable`)
}

Expand All @@ -32,7 +36,7 @@ function astToReact(node, options, parent = {}, index = 0) {
}

function isReactFragment(renderer) {
return React.Fragment && React.Fragment === renderer;
return React.Fragment && React.Fragment === renderer
}

// eslint-disable-next-line max-params, complexity
Expand All @@ -52,11 +56,14 @@ function getNodeProps(node, key, opts, renderer, parent, index) {

// If `includeNodeIndex` is true, pass node index info to all non-tag renderers
if (opts.includeNodeIndex && parent.node && parent.node.children && !isTagRenderer) {
props.index = parent.node.children.indexOf(node);
props.parentChildCount = parent.node.children.length;
props.index = parent.node.children.indexOf(node)
props.parentChildCount = parent.node.children.length
}

const ref = (node.identifier !== null && node.identifier !== undefined) ? opts.definitions[node.identifier] || {} : null
const ref =
node.identifier !== null && node.identifier !== undefined
? opts.definitions[node.identifier] || {}
: null

switch (node.type) {
case 'root':
Expand Down Expand Up @@ -97,15 +104,22 @@ function getNodeProps(node, key, opts, renderer, parent, index) {
case 'link':
assignDefined(props, {
title: node.title || undefined,
target: typeof opts.linkTarget === 'function' ? opts.linkTarget(node.url, node.children, node.title) : opts.linkTarget,
href: opts.transformLinkUri ? opts.transformLinkUri(node.url, node.children, node.title) : node.url
target:
typeof opts.linkTarget === 'function'
? opts.linkTarget(node.url, node.children, node.title)
: opts.linkTarget,
href: opts.transformLinkUri
? opts.transformLinkUri(node.url, node.children, node.title)
: node.url
})
break
case 'image':
assignDefined(props, {
alt: node.alt || undefined,
title: node.title || undefined,
src: opts.transformImageUri ? opts.transformImageUri(node.url, node.children, node.title, node.alt) : node.url
src: opts.transformImageUri
? opts.transformImageUri(node.url, node.children, node.title, node.alt)
: node.url
})
break
case 'linkReference':
Expand Down Expand Up @@ -151,11 +165,9 @@ function getNodeProps(node, key, opts, renderer, parent, index) {
props.skipHtml = opts.skipHtml
break
case 'parsedHtml': {
let parsedChildren;
let parsedChildren
if (node.children) {
parsedChildren = node.children.map(
(child, i) => astToReact(child, opts, {node, props}, i)
)
parsedChildren = node.children.map((child, i) => astToReact(child, opts, {node, props}, i))
}
props.escapeHtml = opts.escapeHtml
props.skipHtml = opts.skipHtml
Expand All @@ -168,8 +180,8 @@ function getNodeProps(node, key, opts, renderer, parent, index) {
xtend(node, {
type: undefined,
position: undefined,
children: undefined,
}),
children: undefined
})
)
}

Expand Down Expand Up @@ -210,14 +222,14 @@ function flattenPosition(pos) {

function getListItemChildren(node, parent) {
if (node.loose) {
return node.children;
return node.children
}

if (parent.node && node.index > 0 && parent.node.children[node.index - 1].loose) {
return node.children;
return node.children
}

return unwrapParagraphs(node);
return unwrapParagraphs(node)
}

function unwrapParagraphs(node) {
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/disallow-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ function untangle(node, index, parent, mode) {
if (mode === 'remove') {
parent.children.splice(index, 1)
} else if (mode === 'unwrap') {
let args = [index, 1];
let args = [index, 1]

if (node.children) {
args = args.concat(node.children);
args = args.concat(node.children)
}

Array.prototype.splice.apply(parent.children, args);
Array.prototype.splice.apply(parent.children, args)
}
}
4 changes: 3 additions & 1 deletion src/plugins/html-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ function parsedHtml(fromNode, toNode, parent) {

module.exports = function getHtmlParserPlugin(config, props) {
if (props && (typeof config.source !== 'undefined' || typeof config.children !== 'undefined')) {
throw new Error('react-markdown: `html-parser` must be called before use - see https://github.com/rexxars/react-markdown#parsing-html')
throw new Error(
'react-markdown: `html-parser` must be called before use - see https://github.com/rexxars/react-markdown#parsing-html'
)
}

const htmlConfig = xtend(defaultConfig, config || {})
Expand Down
6 changes: 3 additions & 3 deletions src/react-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const allTypes = Object.keys(defaultRenderers)

const ReactMarkdown = function ReactMarkdown(props) {
const src = props.source || props.children || ''
const parserOptions = props.parserOptions;
const parserOptions = props.parserOptions

if (props.allowedTypes && props.disallowedTypes) {
throw new Error('Only one of `allowedTypes` and `disallowedTypes` should be defined')
Expand Down Expand Up @@ -86,7 +86,7 @@ ReactMarkdown.defaultProps = {
transformLinkUri: uriTransformer,
astPlugins: [],
plugins: [],
parserOptions: {},
parserOptions: {}
}

ReactMarkdown.propTypes = {
Expand All @@ -107,7 +107,7 @@ ReactMarkdown.propTypes = {
unwrapDisallowed: PropTypes.bool,
renderers: PropTypes.object,
plugins: PropTypes.array,
parserOptions: PropTypes.object,
parserOptions: PropTypes.object
}

ReactMarkdown.types = allTypes
Expand Down
4 changes: 1 addition & 3 deletions src/renderers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ module.exports = {
}

function TextRenderer(props) {
return supportsStringRender
? props.children
: createElement('span', null, props.children)
return supportsStringRender ? props.children : createElement('span', null, props.children)
}

function Root(props) {
Expand Down
53 changes: 29 additions & 24 deletions test/react-markdown.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env jest */
/* eslint-disable react/prop-types */
const Enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');
const Enzyme = require('enzyme')
const Adapter = require('enzyme-adapter-react-16')
const fs = require('fs')
const path = require('path')
const React = require('react')
Expand All @@ -13,7 +13,7 @@ const htmlParser = require('../src/plugins/html-parser')
const Markdown = require('../src/react-markdown')
const MarkdownWithHtml = require('../src/with-html')

Enzyme.configure({ adapter: new Adapter() })
Enzyme.configure({adapter: new Adapter()})

const renderHTML = input => ReactDom.renderToStaticMarkup(input).replace(/^<div>|<\/div>$/g, '')

Expand Down Expand Up @@ -84,7 +84,7 @@ test('should use target attribute for links if specified', () => {

test('should call function to get target attribute for links if specified', () => {
const input = 'This is [a link](https://espen.codes/) to Espen.Codes.'
const getTarget = (uri) => uri.match(/^http/) ? '_blank' : undefined;
const getTarget = uri => (uri.match(/^http/) ? '_blank' : undefined)
const component = renderer.create(<Markdown linkTarget={getTarget} source={input} />)
expect(component.toJSON()).toMatchSnapshot()
})
Expand Down Expand Up @@ -267,9 +267,7 @@ test('should be able to render inline html properly with HTML parser plugin', ()

test('should be able to render inline html properly with HTML parser plugin (through require)', () => {
const input = 'I am having <span class="foo">so</span> much fun'
const component = renderer.create(
<MarkdownWithHtml source={input} escapeHtml={false} />
)
const component = renderer.create(<MarkdownWithHtml source={input} escapeHtml={false} />)
expect(component.toJSON()).toMatchSnapshot()
})

Expand Down Expand Up @@ -299,9 +297,7 @@ test('should be able to render inline html with self-closing tags with attribute

test('should be able to render inline html with self-closing tags with attributes properly with HTML parser plugin (#2)', () => {
const input = 'I am having <wbr class="foo"/> so much fun'
Enzyme.mount(
<Markdown source={input} escapeHtml={false} astPlugins={[htmlParser()]} />
)
Enzyme.mount(<Markdown source={input} escapeHtml={false} astPlugins={[htmlParser()]} />)
})

test('should be able to render multiple inline html elements with self-closing tags with attributes properly with HTML parser plugin', () => {
Expand All @@ -321,7 +317,8 @@ test('should be able to render a table with a single child with HTML parser plug
})

test('should be able to render a table with multiple children with HTML parser plugin', () => {
const input = '<table><thead><tr><th>Title</th></tr></thead><tbody><tr><td>I am having so much fun</td></tr></tbody></table>'
const input =
'<table><thead><tr><th>Title</th></tr></thead><tbody><tr><td>I am having so much fun</td></tr></tbody></table>'
const component = renderer.create(
<Markdown source={input} escapeHtml={false} astPlugins={[htmlParser()]} />
)
Expand All @@ -334,7 +331,7 @@ test('should be able to render replaced non-void html elements with HTML parser
isValidNode: () => true,
processingInstructions: [
{
shouldProcessNode: ({ name }) => name === 'code',
shouldProcessNode: ({name}) => name === 'code',
// eslint-disable-next-line react/display-name
processNode: (node, children) => <kbd>{children}</kbd>
}
Expand Down Expand Up @@ -400,7 +397,9 @@ test('should skip html blocks if skipHtml prop is set (with HTML parser plugin)'
' regular paragraph.'
].join('')

const component = renderer.create(<Markdown source={input} escapeHtml={false} skipHtml astPlugins={[htmlParser()]} />)
const component = renderer.create(
<Markdown source={input} escapeHtml={false} skipHtml astPlugins={[htmlParser()]} />
)
expect(component.toJSON()).toMatchSnapshot()
})

Expand All @@ -411,7 +410,9 @@ test('should escape html blocks if escapeHtml prop is set (with HTML parser plug
' regular paragraph.'
].join('')

const component = renderer.create(<Markdown source={input} escapeHtml astPlugins={[htmlParser()]} />)
const component = renderer.create(
<Markdown source={input} escapeHtml astPlugins={[htmlParser()]} />
)
expect(component.toJSON()).toMatchSnapshot()
})

Expand All @@ -433,7 +434,9 @@ test('should handle html blocks with HTML parser plugin', () => {
' regular paragraph.'
].join('')

const component = renderer.create(<Markdown source={input} escapeHtml={false} astPlugins={[htmlParser()]} />)
const component = renderer.create(
<Markdown source={input} escapeHtml={false} astPlugins={[htmlParser()]} />
)
expect(component.toJSON()).toMatchSnapshot()
})

Expand Down Expand Up @@ -514,7 +517,7 @@ test('should render link references', () => {
})

test('should render empty link references', () => {
const input = 'Stuff were changed in [][]. Check out the changelog for reference.';
const input = 'Stuff were changed in [][]. Check out the changelog for reference.'

expect(renderHTML(<Markdown source={input} />)).toMatchSnapshot()
})
Expand Down Expand Up @@ -751,31 +754,33 @@ test('should be able to override text renderer', () => {
})

test('should pass the key to an overriden text renderer', () => {
const textRenderer = (props) => {
expect(props.nodeKey).toEqual('text-1-1');
return <marquee key={props.nodeKey}>{props.children}</marquee>;
const textRenderer = props => {
expect(props.nodeKey).toEqual('text-1-1')
return <marquee key={props.nodeKey}>{props.children}</marquee>
}

renderer.create(<Markdown source={'foo'} renderers={{ text: textRenderer }} />)
renderer.create(<Markdown source={'foo'} renderers={{text: textRenderer}} />)
})

test('should pass index of a node under its parent to non-tag renderers if includeNodeIndex option is enabled', () => {
const input = 'Foo\n\nBar\n\nBaz'
const paragraph = props => {
expect(props).toMatchSnapshot()
return <p>{props.children}</p>
};
}

const component = renderer.create(<Markdown renderers={{ paragraph }} source={input} includeNodeIndex />)
const component = renderer.create(
<Markdown renderers={{paragraph}} source={input} includeNodeIndex />
)
expect(component.toJSON()).toMatchSnapshot()
})

test('should be able to override remark-parse plugin options', () => {
// gfm is used by default in remark-parse which will not autolink URLs
// containing a space unless the pedantic option is set to true.
const input = '[Spaces in URLs](https://example.com/so much space "Title")'
const pedantic = renderer.create(<Markdown source={input} parserOptions={{ pedantic: true }} />)
const unscholarly = renderer.create(<Markdown source={input} parserOptions={{ pedantic: false }} />)
const pedantic = renderer.create(<Markdown source={input} parserOptions={{pedantic: true}} />)
const unscholarly = renderer.create(<Markdown source={input} parserOptions={{pedantic: false}} />)

expect(pedantic.toJSON()).toMatchSnapshot()
expect(unscholarly.toJSON()).not.toBe(pedantic.toJSON())
Expand Down

0 comments on commit 182f34c

Please sign in to comment.