packages/@textlint/text-to-ast/src/plaintext-parser.js
function parse(text) {
    const textLineByLine = text.split(LINEBREAKE_MARK);
    // it should be alternately Str and Break
    let startIndex = 0;
    const lastLineIndex = textLineByLine.length - 1;
    const isLasEmptytLine = (line, index) => {
        return index === lastLineIndex && line === "";
    };
    const isEmptyLine = (line, index) => {
        return index !== lastLineIndex && line === "";
    };
    const children = textLineByLine.reduce(function(result, currentLine, index) {
        const lineNumber = index + 1;
        if (isLasEmptytLine(currentLine, index)) {
            return result;
        }
        // \n
        if (isEmptyLine(currentLine, index)) {
            const emptyBreakNode = createBRNode(lineNumber, startIndex);
            startIndex += emptyBreakNode.raw.length;
            result.push(emptyBreakNode);
            return result;
        }

        // (Paragraph > Str) -> Br?
        const strNode = parseLine(currentLine, lineNumber, startIndex);
        const paragraph = createParagraph([strNode]);
        startIndex += paragraph.raw.length;
        result.push(paragraph);
        if (index !== lastLineIndex) {
            const breakNode = createEndedBRNode(paragraph);
            startIndex += breakNode.raw.length;
            result.push(breakNode);
        }
        return result;
    }, []);
    return {
        type: Syntax.Document,
        raw: text,
        range: [0, text.length],
        loc: {
            start: {
                line: 1,
                column: 0
            },
            end: {
                line: textLineByLine.length,
                column: textLineByLine[textLineByLine.length - 1].length
            }
        },
        children
    };
}
function parse(text) {
    const textLineByLine = text.split(LINEBREAKE_MARK);
    // it should be alternately Str and Break
    let startIndex = 0;
    const lastLineIndex = textLineByLine.length - 1;
    const isLasEmptytLine = (line, index) => {
        return index === lastLineIndex && line === "";
    };
    const isEmptyLine = (line, index) => {
        return index !== lastLineIndex && line === "";
    };
    const children = textLineByLine.reduce(function(result, currentLine, index) {
        const lineNumber = index + 1;
        if (isLasEmptytLine(currentLine, index)) {
            return result;
        }
        // \n
        if (isEmptyLine(currentLine, index)) {
            const emptyBreakNode = createBRNode(lineNumber, startIndex);
            startIndex += emptyBreakNode.raw.length;
            result.push(emptyBreakNode);
            return result;
        }

        // (Paragraph > Str) -> Br?
        const strNode = parseLine(currentLine, lineNumber, startIndex);
        const paragraph = createParagraph([strNode]);
        startIndex += paragraph.raw.length;
        result.push(paragraph);
        if (index !== lastLineIndex) {
            const breakNode = createEndedBRNode(paragraph);
            startIndex += breakNode.raw.length;
            result.push(breakNode);
        }
        return result;
    }, []);
    return {
        type: Syntax.Document,
        raw: text,
        range: [0, text.length],
        loc: {
            start: {
                line: 1,
                column: 0
            },
            end: {
                line: textLineByLine.length,
                column: textLineByLine[textLineByLine.length - 1].length
            }
        },
        children
    };
}
function parse ( text ) { const textLineByLine = text . split ( LINEBREAKE_MARK ) ; // it should be alternately Str and Break let startIndex = 0 ; const lastLineIndex = textLineByLine . length - 1 ; const isLasEmptytLine = ( line , index ) => { return index === lastLineIndex && line === "" ; } ; const isEmptyLine = ( line , index ) => { return index !== lastLineIndex && line === "" ; } ; const children = textLineByLine . reduce ( function ( result , currentLine , index ) { const lineNumber = index + 1 ; if ( isLasEmptytLine ( currentLine , index ) ) { return result ; } // \n if ( isEmptyLine ( currentLine , index ) ) { const emptyBreakNode = createBRNode ( lineNumber , startIndex ) ; startIndex += emptyBreakNode . raw . length ; result . push ( emptyBreakNode ) ; return result ; } // (Paragraph > Str) -> Br? const strNode = parseLine ( currentLine , lineNumber , startIndex ) ; const paragraph = createParagraph ( [ strNode ] ) ; startIndex += paragraph . raw . length ; result . push ( paragraph ) ; if ( index !== lastLineIndex ) { const breakNode = createEndedBRNode ( paragraph ) ; startIndex += breakNode . raw . length ; result . push ( breakNode ) ; } return result ; } , [ ] ) ; return { type : Syntax . Document , raw : text , range : [ 0 , text . length ] , loc : { start : { line : 1 , column : 0 } , end : { line : textLineByLine . length , column : textLineByLine [ textLineByLine . length - 1 ] . length } } , children } ; }
parse text and return ast mapped location info.
@param {string} text
@returns {TxtNode}
parse text and return ast mapped location info .