Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}),
// Make paragraphs output raw <p> tags, instead of
// </p><div class="paragraph"> tags:
paragraph: aug({}, defaultRules.paragraph, {
output: function(node, outputter) {
return React.createElement(
"p",
null,
outputter(node.content)
);
}
})
});
var rawBuiltParser = SimpleMarkdown.parserFor(rules);
var parse = function(source) {
var blockSource = source + "\n\n";
return rawBuiltParser(blockSource, {inline: false});
};
var output = SimpleMarkdown.outputFor(SimpleMarkdown.ruleOutput(rules));
module.exports = {
parse: parse,
output: output
};
</div>
const SimpleMarkdown = require("simple-markdown");
const arrayRules = {
fence: {
match: SimpleMarkdown.defaultRules.fence.match,
order: 1,
parse: (capture, state, parse) => capture[3],
},
paragraph: {
match: SimpleMarkdown.defaultRules.paragraph.match,
order: 2,
parse: (capture, state, parse) => capture[1],
},
};
const builtArrayParser = SimpleMarkdown.parserFor(arrayRules);
// This should just return an array of strings! magick!
const parseToArray = source => {
// Remove any leading newlines to avoid splitting weirdness
// (simple-markdown has the `newline` rule for this, and i have
// no idea how this will handle leading newlines without that rule),
// and add \n\n to let it parse at a block/paragraph level
const paragraphedSource = source.replace(/^\n\s*\n/, "") + "\n\n";
return builtArrayParser(paragraphedSource, {inline: false});
};
const joinFromArray = paragraphs => paragraphs.join("\n\n");
module.exports = {
parseToArray: parseToArray,
joinFromArray: joinFromArray,
return
},
},
paragraph: {
...SimpleMarkdown.defaultRules.paragraph,
react: (node, output, state) => {
return (
{output(node.content, state)}
)
},
},
}
// Markdown parser setup
const rawBuiltParser = SimpleMarkdown.parserFor(rules)
const parser = source => {
const blockSource = source + "\n\n"
return rawBuiltParser(blockSource, { inline: false })
}
const reactOutput = SimpleMarkdown.reactFor(SimpleMarkdown.ruleOutput(rules, "react"))
export const toReact = md => {
const syntaxTree = parser(md)
return reactOutput(syntaxTree)
}
interface NativeMarkdownProps {
md: string
}
export class MarkdownRenderer extends React.Component {
_renderContent = (children: string): React$Element => {
try {
const mergedStyles = Object.assign(initialStyles, this.props.styles)
const rules = this._postProcessRules(
_.merge(
{},
SimpleMarkdown.defaultRules,
initialRules(mergedStyles),
this.props.rules
)
)
const child = Array.isArray(this.props.children)
? this.props.children.join('')
: this.props.children
const blockSource = child + '\n\n'
const tree = SimpleMarkdown.parserFor(rules)(blockSource, {
inline: false
})
return SimpleMarkdown.reactFor(SimpleMarkdown.ruleOutput(rules, 'react'))(
tree
)
} catch (errors) {
this.props.errorHandler
? this.props.errorHandler(errors, children)
: console.error(errors)
}
}
content: "@unknown-role",
}),
react: null,
},
channelMention: {
order: defaultRules.text.order,
match: inlineRegex(/^<#\d+>/),
parse: () => ({
type: "mention",
content: "#unknown-channel",
}),
react: null,
},
}
const parseInline = parserFor(inlineRules, { inline: true })
const parseBlock = parserFor(blockRules, { inline: true })
const reactOutput = outputFor({ ...inlineRules, ...blockRules }, "react")
const ellipsize = (text: string, length: number) => {
const shortenedText = text.replace(/\s+/g, " ")
return shortenedText.length <= length
? shortenedText
: `${shortenedText.slice(0, length)}…`
}
const now = () => (typeof performance === "undefined" ? 0 : performance.now())
export const parseMarkup = (
content: string,
options?: { inline?: boolean; jumboable?: boolean },
) => {
function parserFor(rules, returnAst) {
const parser = SimpleMarkdown.parserFor(rules);
const renderer = SimpleMarkdown.reactFor(SimpleMarkdown.ruleOutput(rules, 'react'));
return function(input='', inline=true, state={}, transform=null) {
if (!inline) {
input += '\n\n';
}
let ast = parser(input, { inline, ...state });
ast = flattenAst(ast);
if (transform) {
ast = transform(ast);
}
if (returnAst) {
return ast;
}
const opts = {
enableLightBox: props.enableLightBox,
navigator: props.navigator,
imageParam: props.imageParam,
onLink: props.onLink,
bgImage: props.bgImage,
onImageOpen: props.onImageOpen,
onImageClose: props.onImageClose,
};
const mergedStyles = merge({}, styles, props.styles);
var rules = require('./rules')(mergedStyles, opts);
rules = merge({}, SimpleMarkdown.defaultRules, rules);
const parser = SimpleMarkdown.parserFor(rules);
this.parse = function (source) {
const blockSource = source + '\n\n';
return parser(blockSource, {inline: false});
};
this.renderer = SimpleMarkdown.reactFor(SimpleMarkdown.ruleOutput(rules, 'react'));
}
constructor(props) {
super(props);
const rules = SimpleMarkdown.defaultRules;
this.parser = SimpleMarkdown.parserFor(rules);
this.reactOutput = SimpleMarkdown.reactFor(SimpleMarkdown.ruleOutput(rules, 'react'));
const blockSource = this.props.children + '\n\n';
const parseTree = this.parser(blockSource, { inline: this.props.parseInline });
const outputResult = this.reactOutput(parseTree);
const defaultStyles = this.props.useDefaultStyles && styles ? styles : {};
const _styles = StyleSheet.create(Object.assign(defaultStyles, this.props.markdownStyles));
this.state = {
syntaxTree: outputResult,
styles: _styles
};
}
render() {
const {rules = {}, styles = {}, onLinkPress} = this.props
const mergedStyles = mergeStyles(DefaultStyles, styles)
const mergedRules = mergeRules(SimpleMarkdown.defaultRules, simpleMarkdownRules(mergeRules(DefaultRules, rules), mergedStyles))
const markdown = (Array.isArray(this.props.children) ? this.props.children.join('') : this.props.children) + '\n\n'
const ast = SimpleMarkdown.parserFor(mergedRules)(markdown, {inline: false})
const render = SimpleMarkdown.reactFor(SimpleMarkdown.ruleOutput(mergedRules, 'react'))
const initialRenderState = {onLinkPress: onLinkPress}
return (
)
}
}