Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
d25469f
feat(markdown): inital implementation
ikatyang Sep 21, 2017
50aeda5
feat(markdown): support strong
ikatyang Sep 21, 2017
63de920
fix: add missing default value
ikatyang Sep 21, 2017
41d0f43
feat(markdown): support inlineCode
ikatyang Sep 21, 2017
f1cce5b
feat: support delete
ikatyang Sep 21, 2017
a59dbcc
feat: support link
ikatyang Sep 21, 2017
1ae442a
feat: support image
ikatyang Sep 21, 2017
c8308ee
feat: support blockquote
ikatyang Sep 21, 2017
1e41713
feat: support heading
ikatyang Sep 22, 2017
703db97
feat: support code
ikatyang Sep 22, 2017
956eca7
feat: support yaml
ikatyang Sep 22, 2017
3085364
feat: support html
ikatyang Sep 22, 2017
57d549b
feat: support list
ikatyang Sep 22, 2017
0b42021
feat: support thematicBreak
ikatyang Sep 22, 2017
9fb9f71
feat: support table
ikatyang Sep 22, 2017
9b85280
feat: support linkReference
ikatyang Sep 22, 2017
032a1c0
feat: support imageReference
ikatyang Sep 22, 2017
fba4662
feat: support definition
ikatyang Sep 22, 2017
a982f35
feat: support footnote
ikatyang Sep 22, 2017
219356e
feat: support footnoteReference
ikatyang Sep 22, 2017
cecd425
feat: support footnoteDefinition
ikatyang Sep 22, 2017
bbd7837
test(cli): update snapshots
ikatyang Sep 22, 2017
efa96e3
refactor: extract SINGLE_LINE_NODE_TYPES
ikatyang Sep 25, 2017
709093f
refactor: printChildren
ikatyang Sep 25, 2017
f764d72
fix: correct newlines
ikatyang Sep 26, 2017
f31025a
test: add trailing newline
ikatyang Sep 26, 2017
9a99ac7
fix: blockquote formatting
ikatyang Sep 27, 2017
5eead94
fix: node types
ikatyang Sep 27, 2017
a5c64cb
fix: break line correctly
ikatyang Sep 28, 2017
7c15481
fix: remove unnecessary properties to make AST_COMPARE happy
ikatyang Sep 28, 2017
2608853
fix: escape `|` in tableCell content
ikatyang Sep 28, 2017
e9d0ed9
fix: unexpected line break
ikatyang Sep 28, 2017
9609465
fix: ast difference from loose list
ikatyang Sep 28, 2017
3cb24e7
fix: html break lines
ikatyang Sep 29, 2017
232dc7f
Merge branch 'master' into feat/markdown
ikatyang Sep 30, 2017
ec302e6
refactor: fix linting
ikatyang Sep 30, 2017
6b1ed27
fix: normalize ast
ikatyang Sep 30, 2017
78327ad
fix: escape specific chars
ikatyang Sep 30, 2017
19c2ae7
test: add more tests
ikatyang Sep 30, 2017
1271883
fix: build markdown parser
ikatyang Sep 30, 2017
e9c68b1
chore: remove unnecessary *.log
ikatyang Oct 1, 2017
dc12b70
fix: escape html entity
ikatyang Oct 1, 2017
01ce9ff
feat: support prettier-ignore
ikatyang Oct 1, 2017
0b442c1
fix: line break for non-loose listItem
ikatyang Oct 1, 2017
066599b
feat: support formatting `code` based on `lang`
ikatyang Oct 1, 2017
e1ca36d
fix: add `jsx` and `tsx`
ikatyang Oct 1, 2017
505fe5a
fix: use multiparser
ikatyang Oct 1, 2017
0e8ecdf
refactor: fix linting
ikatyang Oct 1, 2017
f485b2a
test: update test case 😉
ikatyang Oct 1, 2017
acea7f7
feat: switch to `_` style emphasis
ikatyang Oct 1, 2017
ee546a5
fix: sequence list should use different prefix
ikatyang Oct 1, 2017
461c97c
test: add tests
ikatyang Oct 1, 2017
0c4b3a7
fix: do not print additional new line after `prettier-ignore`
ikatyang Oct 1, 2017
a1f9761
fix(list): enforce `1.` to avoid unnecessary git diff
ikatyang Oct 1, 2017
4f17533
feat: enable `commonmark` option
ikatyang Oct 1, 2017
d0630ad
fix: respect autolink-style link
ikatyang Oct 1, 2017
4143c10
feat: support md`...` and markdown`...`
ikatyang Oct 2, 2017
654d772
Merge branch 'master' into feat/markdown
ikatyang Oct 2, 2017
1f4872d
docs: replace ands with commas
ikatyang Oct 3, 2017
5268a35
fix: respect indented code block
ikatyang Oct 3, 2017
f69dad1
fix: respect html entity
ikatyang Oct 3, 2017
4306f43
docs: add docs for modified MDAST
ikatyang Oct 3, 2017
e57e7d6
fix: inlineCode is breakline-able
ikatyang Oct 3, 2017
05629fe
feat: support backtick in inlineCode
ikatyang Oct 3, 2017
2653cda
feat: support a-lot-of-backtick in fenced code block
ikatyang Oct 3, 2017
1e4f3ae
feat: use `~~~`-style code block in js template
ikatyang Oct 4, 2017
ed0e8c1
fix: respect escaped chars
ikatyang Oct 5, 2017
8a0a90f
test: add test cases
ikatyang Oct 5, 2017
aa5e560
fix: use `- - -`-style thematicBreak to avoid conflict with yaml
ikatyang Oct 5, 2017
7704bb7
fix: remain the same content for linkReference identifier
ikatyang Oct 6, 2017
f670f1f
refactor: fix typo
ikatyang Oct 6, 2017
506f8bc
fix: wrap `definition`'s url if there's whitespace
ikatyang Oct 6, 2017
f1dcfbe
fix: remove unnecessary whitespace at the end of paragraph
ikatyang Oct 6, 2017
7c95809
fix: fix: remove unnecessary whitespace at the start of paragraph
ikatyang Oct 6, 2017
e4a52ac
fix: setence children length is possible 0
ikatyang Oct 6, 2017
b8e43cf
fix: support continuous ordered list
ikatyang Oct 6, 2017
65b8e9a
fix: do not print addtional hardline after loose list
ikatyang Oct 6, 2017
76cc30a
fix: use double-backtick style for single-backtick value in inlineCode
ikatyang Oct 6, 2017
a6a1f05
fix: support nested emphasis
ikatyang Oct 6, 2017
9e3d01a
fix: support space-url in link/image
ikatyang Oct 6, 2017
90d73b8
fix: escape `)` in link/image url
ikatyang Oct 6, 2017
0acc85d
fix: support single-quote in link/image/definition title
ikatyang Oct 6, 2017
2a54bcd
fix: respect alt in image/imageReference
ikatyang Oct 6, 2017
52ca697
fix: use `*`-style thematicBreak in list
ikatyang Oct 6, 2017
a29a73c
fix: loose/tight list linebreaks
ikatyang Oct 6, 2017
1f7a8d1
fix: print third linebreak before indented code block with a tight li…
ikatyang Oct 6, 2017
53d6c95
test: move bug cases
ikatyang Oct 6, 2017
ea98870
fix: remove unnecessary linebreaks
ikatyang Oct 6, 2017
e196a12
Merge branch 'master' into feat/markdown
ikatyang Oct 6, 2017
d82febe
refactor: fix typo
ikatyang Oct 7, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/node_modules
npm-debug.log
*.log
/errors
/test.js
/test.ts
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"cosmiconfig": "3.1.0",
"dashify": "0.2.2",
"diff": "3.2.0",
"escape-string-regexp": "1.0.5",
"esutils": "2.0.2",
"flow-parser": "0.51.0",
"get-stream": "3.0.0",
Expand All @@ -39,9 +40,12 @@
"postcss-scss": "1.0.0",
"postcss-selector-parser": "2.2.3",
"postcss-values-parser": "1.3.1",
"remark-frontmatter": "1.1.0",
"remark-parse": "4.0.0",
"strip-bom": "3.0.0",
"typescript": "2.5.3",
"typescript-eslint-parser": "git://github.com/eslint/typescript-eslint-parser.git#9c71a627da36e97da52ed2731d58509c952b67ae"
"typescript-eslint-parser": "git://github.com/eslint/typescript-eslint-parser.git#9c71a627da36e97da52ed2731d58509c952b67ae",
"unified": "6.1.5"
},
"devDependencies": {
"babel-cli": "6.24.1",
Expand Down
3 changes: 2 additions & 1 deletion scripts/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const parsers = [
"typescript",
"graphql",
"postcss",
"parse5"
"parse5",
"markdown"
];

process.env.PATH += path.delimiter + path.join(rootDir, "node_modules", ".bin");
Expand Down
13 changes: 10 additions & 3 deletions src/clean-ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,17 @@ function massageAST(ast, parent) {
"after",
"trailingComma",
"parent",
"prev"
"prev",
"position"
].forEach(name => {
delete newObj[name];
});

// for markdown codeblock
if (ast.type === "code") {
delete newObj.value;
}

if (
ast.type === "media-query" ||
ast.type === "media-query-list" ||
Expand Down Expand Up @@ -222,14 +228,15 @@ function massageAST(ast, parent) {
quasis.forEach(q => delete q.value);
}

// styled-components and graphql
// styled-components, graphql, markdown
if (
ast.type === "TaggedTemplateExpression" &&
(ast.tag.type === "MemberExpression" ||
(ast.tag.type === "Identifier" &&
(ast.tag.name === "gql" ||
ast.tag.name === "graphql" ||
ast.tag.name === "css")) ||
ast.tag.name === "css" ||
ast.tag.name === "md")) ||
ast.tag.type === "CallExpression")
) {
newObj.quasi.quasis.forEach(quasi => delete quasi.value);
Expand Down
3 changes: 2 additions & 1 deletion src/cli-constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ const detailedOptions = normalizeDetailedOptions({
"less",
"scss",
"json",
"graphql"
"graphql",
"markdown"
],
description: "Which parser to use.",
getter: (value, argv) => (argv["flow-parser"] ? "flow" : value)
Expand Down
20 changes: 10 additions & 10 deletions src/doc-printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ function rootIndent() {
return {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why these changes were required?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use for blockquote (leading > )

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent: 0,
align: {
spaces: 0,
tabs: 0
spaces: "",
tabs: ""
}
};
}
Expand All @@ -30,17 +30,17 @@ function makeAlign(ind, n) {
return {
indent: 0,
align: {
spaces: 0,
tabs: 0
spaces: "",
tabs: ""
}
};
}

return {
indent: ind.indent,
align: {
spaces: ind.align.spaces + n,
tabs: ind.align.tabs + (n ? 1 : 0)
spaces: ind.align.spaces + (typeof n === "number" ? " ".repeat(n) : n),
tabs: ind.align.tabs + (n ? "\t" : "")
}
};
}
Expand Down Expand Up @@ -399,12 +399,12 @@ function printDocToString(doc, options) {
}
}

const length = ind.indent * options.tabWidth + ind.align.spaces;
const indentLength = ind.indent * options.tabWidth;
const indentString = options.useTabs
? "\t".repeat(ind.indent + ind.align.tabs)
: " ".repeat(length);
? "\t".repeat(ind.indent) + ind.align.tabs
: " ".repeat(indentLength) + ind.align.spaces;
out.push(newLine + indentString);
pos = length;
pos = indentLength + ind.align.spaces.length;
}
break;
}
Expand Down
104 changes: 104 additions & 0 deletions src/multiparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,59 @@ function getSubtreeParser(path, options) {
case "flow":
case "typescript":
return fromBabylonFlowOrTypeScript(path, options);
case "markdown":
return fromMarkdown(path, options);
}
}

function fromMarkdown(path, options) {
const node = path.getValue();

if (node.type === "code") {
const parser = getParserName(node.lang);
if (parser) {
const styleUnit = options.__inJsTemplate ? "~" : "`";
const style = styleUnit.repeat(
Math.max(3, util.getMaxContinuousCount(node.value, styleUnit) + 1)
);
return {
options: { parser },
transformDoc: doc => concat([style, node.lang, hardline, doc, style]),
text: node.value
};
}
}

return null;

function getParserName(lang) {
switch (lang) {
case "js":
case "jsx":
case "javascript":
return "babylon";
case "ts":
case "tsx":
case "typescript":
return "typescript";
case "gql":
case "graphql":
return "graphql";
case "css":
return "css";
case "less":
return "less";
case "scss":
return "scss";
case "json":
case "json5":
return "json";
case "md":
case "markdown":
return "markdown";
default:
return null;
}
}
}

Expand Down Expand Up @@ -92,11 +145,62 @@ function fromBabylonFlowOrTypeScript(path) {
};
}

/**
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be the first time it is possible to infinitely nest the multiparser...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

markdown`
     ```js
     markdown`
          ```js
          console.log('hi');
          ```
     `
     ```
`

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think we should use ~~~-style code block for markdown in template string.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't even know that existed!

* md`...`
* markdown`...`
*/
if (
parentParent &&
(parentParent.type === "TaggedTemplateExpression" &&
parent.quasis.length === 1 &&
(parentParent.tag.type === "Identifier" &&
(parentParent.tag.name === "md" ||
parentParent.tag.name === "markdown")))
) {
return {
options: { parser: "markdown", __inJsTemplate: true },
transformDoc: doc =>
concat([
indent(
concat([softline, stripTrailingHardline(escapeBackticks(doc))])
),
softline
]),
// leading whitespaces matter in markdown
text: dedent(parent.quasis[0].value.cooked)
};
}

break;
}
}
}

function dedent(str) {
const spaces = str.match(/\n^( *)/m)[1].length;
return str.replace(new RegExp(`^ {${spaces}}`, "gm"), "").trim();
}

function escapeBackticks(doc) {
return util.mapDoc(doc, currentDoc => {
if (!currentDoc.parts) {
return currentDoc;
}

const parts = [];

currentDoc.parts.forEach(part => {
if (typeof part === "string") {
parts.push(part.replace(/`/g, "\\`"));
} else {
parts.push(part);
}
});

return Object.assign({}, currentDoc, { parts });
});
}

function fromHtmlParser2(path, options) {
const node = path.getValue();

Expand Down
2 changes: 2 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ function normalize(options) {
normalized.parser = "graphql";
} else if (/\.json$/.test(filepath)) {
normalized.parser = "json";
} else if (/\.(md|markdown)$/.test(filepath)) {
normalized.parser = "markdown";
}

if (normalized.parser === "json") {
Expand Down
Loading