/**
 * Paragraph parsing/splitting for article jipt i18n
 */

const SimpleMarkdown = require("simple-markdown");

const arrayRules = {
    paragraph: {
        match: SimpleMarkdown.defaultRules.paragraph.match,
        order: 1,
        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,
};

Add a small paragraph-splitting parser.
JIPT: Add a small paragraph-splitting parser.

Summary:
Uses simple-markdown to add a function to split a string into its
constituent paragraphs, and a function to join those paragraphs back
into a source string.

Depends on D21241

Test Plan: none

Reviewers: emily

Reviewed By: emily

Subscribers: jlfwong, kevinb

Differential Revision: https://phabricator.khanacademy.org/D21242
