prettier/src/language-markdown/parser-markdown.js

401 lines
9.5 KiB
JavaScript
Raw Normal View History

feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
"use strict";
const remarkParse = require("remark-parse");
const unified = require("unified");
2018-05-24 21:30:45 +03:00
const pragma = require("./pragma");
const parseFrontMatter = require("../utils/front-matter");
const util = require("../common/util");
const { getOrderedListItemInfo } = require("./utils");
const mdx = require("./mdx");
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
// 0x0 ~ 0x10ffff
const isSingleCharRegex = /^([\u0000-\uffff]|[\ud800-\udbff][\udc00-\udfff])$/;
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
/**
* based on [MDAST](https://github.com/syntax-tree/mdast) with following modifications:
*
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
* 1. restore unescaped character (Text)
* 2. merge continuous Texts
* 3. replace whitespaces in InlineCode#value with one whitespace
* reference: http://spec.commonmark.org/0.25/#example-605
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
* 4. split Text into Sentence
*
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
* interface Word { value: string }
* interface Whitespace { value: string }
* interface Sentence { children: Array<Word | Whitespace> }
* interface InlineCode { children: Array<Sentence> }
*/
function createParse({ isMDX }) {
return (text, parsers, opts) => {
const processor = unified()
.use(
remarkParse,
Object.assign(
{
footnotes: true,
commonmark: true
},
isMDX && { blocks: [mdx.BLOCKS_REGEX] }
)
)
.use(frontMatter)
.use(isMDX ? mdx.esSyntax : identity)
.use(liquid)
.use(restoreUnescapedCharacter(text))
.use(mergeContinuousTexts)
.use(transformInlineCode)
.use(transformIndentedCodeblockAndMarkItsParentList(text))
.use(markAlignedList(text, opts))
.use(splitText(opts))
.use(isMDX ? htmlToJsx : identity)
.use(isMDX ? mergeContinuousImportExport : identity);
return processor.runSync(processor.parse(text));
};
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
}
function map(ast, handler) {
return (function preorder(node, index, parentStack) {
parentStack = parentStack || [];
const newNode = Object.assign({}, handler(node, index, parentStack));
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
if (newNode.children) {
newNode.children = newNode.children.map((child, index) => {
return preorder(child, index, [newNode].concat(parentStack));
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
});
}
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
return newNode;
})(ast, null, null);
}
function identity(x) {
return x;
}
function htmlToJsx() {
return ast =>
map(ast, (node, index, [parent]) => {
if (
node.type !== "html" ||
/^<!--[\s\S]*-->$/.test(node.value) ||
// inline html
parent.type === "paragraph"
) {
return node;
}
return Object.assign({}, node, { type: "jsx" });
});
}
function mergeContinuousImportExport() {
return mergeChildren(
(prevNode, node) =>
prevNode.type === "importExport" && node.type === "importExport",
(prevNode, node) => ({
type: "importExport",
value: prevNode.value + "\n\n" + node.value,
position: {
start: prevNode.position.start,
end: node.position.end
}
})
);
}
function transformInlineCode() {
return ast =>
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
map(ast, node => {
if (node.type !== "inlineCode") {
return node;
}
return Object.assign({}, node, {
value: node.value.replace(/\s+/g, " ")
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
});
});
}
function restoreUnescapedCharacter(originalText) {
return () => ast =>
map(ast, node => {
return node.type !== "text"
? node
: Object.assign({}, node, {
value:
node.value !== "*" &&
node.value !== "_" && // handle these two cases in printer
isSingleCharRegex.test(node.value) &&
node.position.end.offset - node.position.start.offset !==
node.value.length
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
? originalText.slice(
node.position.start.offset,
node.position.end.offset
)
: node.value
});
});
}
function mergeChildren(shouldMerge, mergeNode) {
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
return ast =>
map(ast, node => {
if (!node.children) {
return node;
}
const children = node.children.reduce((current, child) => {
const lastChild = current[current.length - 1];
if (lastChild && shouldMerge(lastChild, child)) {
current.splice(-1, 1, mergeNode(lastChild, child));
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
} else {
current.push(child);
}
return current;
}, []);
return Object.assign({}, node, { children });
});
}
function mergeContinuousTexts() {
return mergeChildren(
(prevNode, node) => prevNode.type === "text" && node.type === "text",
(prevNode, node) => ({
type: "text",
value: prevNode.value + node.value,
position: {
start: prevNode.position.start,
end: node.position.end
}
})
);
}
function splitText(options) {
return () => ast =>
map(ast, (node, index, [parentNode]) => {
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
if (node.type !== "text") {
return node;
}
let value = node.value;
if (parentNode.type === "paragraph") {
if (index === 0) {
value = value.trimLeft();
}
if (index === parentNode.children.length - 1) {
value = value.trimRight();
}
}
return {
type: "sentence",
position: node.position,
children: util.splitText(value, options)
feat: support markdown (#2943) * feat(markdown): inital implementation * feat(markdown): support strong * fix: add missing default value * feat(markdown): support inlineCode * feat: support delete * feat: support link * feat: support image * feat: support blockquote * feat: support heading * feat: support code * feat: support yaml * feat: support html * feat: support list * feat: support thematicBreak * feat: support table * feat: support linkReference * feat: support imageReference * feat: support definition * feat: support footnote * feat: support footnoteReference * feat: support footnoteDefinition * test(cli): update snapshots * refactor: extract SINGLE_LINE_NODE_TYPES * refactor: printChildren * fix: correct newlines * test: add trailing newline * fix: blockquote formatting * fix: node types * fix: break line correctly * fix: remove unnecessary properties to make AST_COMPARE happy * fix: escape `|` in tableCell content * fix: unexpected line break * fix: ast difference from loose list * fix: html break lines * refactor: fix linting * fix: normalize ast * fix: escape specific chars * test: add more tests * fix: build markdown parser * chore: remove unnecessary *.log * fix: escape html entity * feat: support prettier-ignore * fix: line break for non-loose listItem * feat: support formatting `code` based on `lang` * fix: add `jsx` and `tsx` * fix: use multiparser * refactor: fix linting * test: update test case 😉 * feat: switch to `_` style emphasis * fix: sequence list should use different prefix * test: add tests * fix: do not print additional new line after `prettier-ignore` * fix(list): enforce `1.` to avoid unnecessary git diff * feat: enable `commonmark` option * feat: support `break` * fix: escape backslash * refactor: escape html entity using backslash * fix: respect autolink-style link * feat: support md`...` and markdown`...` * docs: replace ands with commas * fix: respect indented code block * fix: respect html entity * docs: add docs for modified MDAST * fix: inlineCode is breakline-able * feat: support backtick in inlineCode * feat: support a-lot-of-backtick in fenced code block * feat: use `~~~`-style code block in js template * fix: respect escaped chars * fix: use `*`-style emphasis for invalid `_`-style output * test: add test cases * fix: use `- - -`-style thematicBreak to avoid conflict with yaml * fix: remain the same content for linkReference identifier * fix: `inlineCode` gap can be a line break * fix: `html` should not print trailing spaces if it's in root * refactor: fix typo * fix: wrap `definition`'s url if there's whitespace * fix: remove unnecessary whitespace at the end of paragraph * fix: fix: remove unnecessary whitespace at the start of paragraph * fix: setence children length is possible 0 * fix: support continuous ordered list * fix: do not print addtional hardline after loose list * fix: use double-backtick style for single-backtick value in inlineCode * fix: support nested emphasis * fix: support space-url in link/image * fix: escape `)` in link/image url * fix: support single-quote in link/image/definition title * fix: respect alt in image/imageReference * fix: use `*`-style thematicBreak in list * fix: loose/tight list linebreaks * fix: print third linebreak before indented code block with a tight list in the previous * test: move bug cases * fix: remove unnecessary linebreaks * refactor: fix typo
2017-10-12 01:46:44 +03:00
};
});
}
function frontMatter() {
2018-05-15 04:17:15 +03:00
const proto = this.Parser.prototype;
proto.blockMethods = ["frontMatter"].concat(proto.blockMethods);
proto.blockTokenizers.frontMatter = tokenizer;
2018-05-15 04:17:15 +03:00
function tokenizer(eat, value) {
const parsed = parseFrontMatter(value);
2018-05-15 04:17:15 +03:00
if (parsed.frontMatter) {
return eat(parsed.frontMatter.raw)(parsed.frontMatter);
2018-05-15 04:17:15 +03:00
}
}
tokenizer.onlyAtStart = true;
}
2018-05-15 20:22:41 +03:00
function liquid() {
const proto = this.Parser.prototype;
const methods = proto.inlineMethods;
methods.splice(methods.indexOf("text"), 0, "liquid");
proto.inlineTokenizers.liquid = tokenizer;
function tokenizer(eat, value) {
const match = value.match(/^({%[\s\S]*?%}|{{[\s\S]*?}})/);
if (match) {
return eat(match[0])({
type: "liquidNode",
value: match[0]
});
}
}
tokenizer.locator = function(value, fromIndex) {
return value.indexOf("{", fromIndex);
};
}
function transformIndentedCodeblockAndMarkItsParentList(originalText) {
return () => ast =>
map(ast, (node, index, parentStack) => {
if (node.type === "code") {
// the first char may point to `\n`, e.g. `\n\t\tbar`, just ignore it
const isIndented = /^\n?( {4,}|\t)/.test(
originalText.slice(
node.position.start.offset,
node.position.end.offset
)
);
node.isIndented = isIndented;
if (isIndented) {
for (let i = 0; i < parentStack.length; i++) {
const parent = parentStack[i];
// no need to check checked items
if (parent.hasIndentedCodeblock) {
break;
}
if (parent.type === "list") {
parent.hasIndentedCodeblock = true;
}
}
}
}
return node;
});
}
function markAlignedList(originalText, options) {
return () => ast =>
map(ast, (node, index, parentStack) => {
if (node.type === "list" && node.children.length !== 0) {
// if one of its parents is not aligned, it's not possible to be aligned in sub-lists
for (let i = 0; i < parentStack.length; i++) {
const parent = parentStack[i];
if (parent.type === "list" && !parent.isAligned) {
node.isAligned = false;
return node;
}
}
node.isAligned = isAligned(node);
}
return node;
});
function getListItemStart(listItem) {
return listItem.children.length === 0
? -1
: listItem.children[0].position.start.column - 1;
}
function isAligned(list) {
if (!list.ordered) {
/**
* - 123
* - 123
*/
return true;
}
const [firstItem, secondItem] = list.children;
const firstInfo = getOrderedListItemInfo(firstItem, originalText);
if (firstInfo.leadingSpaces.length > 1) {
/**
* 1. 123
*
* 1. 123
* 1. 123
*/
return true;
}
const firstStart = getListItemStart(firstItem);
if (firstStart === -1) {
/**
* 1.
*
* 1.
* 1.
*/
return false;
}
if (list.children.length === 1) {
/**
* aligned:
*
* 11. 123
*
* not aligned:
*
* 1. 123
*/
return firstStart % options.tabWidth === 0;
}
const secondStart = getListItemStart(secondItem);
if (firstStart !== secondStart) {
/**
* 11. 123
* 1. 123
*
* 1. 123
* 11. 123
*/
return false;
}
if (firstStart % options.tabWidth === 0) {
/**
* 11. 123
* 12. 123
*/
return true;
}
/**
* aligned:
*
* 11. 123
* 1. 123
*
* not aligned:
*
* 1. 123
* 2. 123
*/
const secondInfo = getOrderedListItemInfo(secondItem, originalText);
return secondInfo.leadingSpaces.length > 1;
}
}
const baseParser = {
2018-05-24 21:30:45 +03:00
astFormat: "mdast",
hasPragma: pragma.hasPragma,
locStart: node => node.position.start.offset,
locEnd: node => node.position.end.offset
};
const markdownParser = Object.assign({}, baseParser, {
parse: createParse({ isMDX: false })
});
const mdxParser = Object.assign({}, baseParser, {
parse: createParse({ isMDX: true })
});
2018-05-24 21:30:45 +03:00
module.exports = {
parsers: {
remark: markdownParser,
2018-05-24 21:30:45 +03:00
// TODO: Delete this in 2.0
markdown: markdownParser,
mdx: mdxParser
2018-05-24 21:30:45 +03:00
}
};