How to use the simple-markdown.parserFor function in simple-markdown

To help you get started, we’ve selected a few simple-markdown examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github WRidder / react-spa / client / src / components / markdown / custom-md.jsx View on Github external
}),

    // 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>
github Khan / perseus / src / jipt-paragraphs.jsx View on Github external
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,
github artsy / emission / src / lib / Components / Bidding / Components / MarkdownRenderer.tsx View on Github external
return 
    },
  },
  paragraph: {
    ...SimpleMarkdown.defaultRules.paragraph,
    react: (node, output, state) =&gt; {
      return (
        
          {output(node.content, state)}
        
      )
    },
  },
}
// Markdown parser setup
const rawBuiltParser = SimpleMarkdown.parserFor(rules)
const parser = source =&gt; {
  const blockSource = source + "\n\n"
  return rawBuiltParser(blockSource, { inline: false })
}
const reactOutput = SimpleMarkdown.reactFor(SimpleMarkdown.ruleOutput(rules, "react"))

export const toReact = md =&gt; {
  const syntaxTree = parser(md)
  return reactOutput(syntaxTree)
}

interface NativeMarkdownProps {
  md: string
}

export class MarkdownRenderer extends React.Component {
github CharlesMangwa / react-native-simple-markdown / index.js View on Github external
_renderContent = (children: string): React$Element =&gt; {
    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)
    }
  }
github jaylineko / discohook / src / markup / parseMarkup.tsx View on Github external
content: "@unknown-role",
    }),
    react: null,
  },
  channelMention: {
    order: defaultRules.text.order,
    match: inlineRegex(/^&lt;#\d+&gt;/),
    parse: () =&gt; ({
      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) =&gt; {
  const shortenedText = text.replace(/\s+/g, " ")
  return shortenedText.length &lt;= length
    ? shortenedText
    : `${shortenedText.slice(0, length)}…`
}

const now = () =&gt; (typeof performance === "undefined" ? 0 : performance.now())

export const parseMarkup = (
  content: string,
  options?: { inline?: boolean; jumboable?: boolean },
) =&gt; {
github leovoel / embed-visualizer / src / components / markdown.jsx View on Github external
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;
    }
github andangrd / react-native-markdown-package / index.js View on Github external
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'));
  }
github lappalj4 / react-native-easy-markdown / index.js View on Github external
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
        };
    }
github Benjamin-Dobell / react-native-markdown-view / MarkdownView.js View on Github external
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 (
      
    )
  }
}