Skip to content

Commit

Permalink
port tight list handling fix from simple-markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
quantizor committed Mar 24, 2018
1 parent 6449223 commit 4312e1d
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 4 deletions.
83 changes: 83 additions & 0 deletions __snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2809,6 +2809,89 @@ exports[`markdown-to-jsx compiler lists should handle an ordered list with a spe
`;
exports[`markdown-to-jsx compiler lists should handle link trees 1`] = `
<ul data-reactroot>
<li>
<a href="#buttermilk">
buttermilk
</a>
<ul>
<li>
<a href="#installation">
installation
</a>
</li>
<li>
<a href="#usage">
usage
</a>
<ul>
<li>
<a href="#configuration">
configuration
</a>
</li>
<li>
<a href="#components">
components
</a>
<ul>
<li>
<a href="#router">
<code>
&lt;Router&gt;
</code>
</a>
</li>
<li>
<a href="#routingstate">
<code>
&lt;RoutingState&gt;
</code>
</a>
</li>
<li>
<a href="#link">
<code>
&lt;Link&gt;
</code>
</a>
</li>
</ul>
</li>
<li>
<a href="#utilities">
utilities
</a>
<ul>
<li>
<a href="#routeurl-string-addnewhistoryentry-boolean--true">
<code>
route(url: String, addNewHistoryEntry: Boolean = true)
</code>
</a>
</li>
</ul>
</li>
<li>
<a href="#holistic-example">
holistic example
</a>
</li>
</ul>
</li>
<li>
<a href="#goals">
goals
</a>
</li>
</ul>
</li>
</ul>
`;
exports[`markdown-to-jsx compiler lists should not add an extra wrapper around a list 1`] = `
<ul data-reactroot>
Expand Down
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ const LINK_AUTOLINK_BARE_URL_R = /^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/;
const LINK_AUTOLINK_MAILTO_R = /^<([^ >]+@[^ >]+)>/;
const LINK_AUTOLINK_R = /^<([^ >]+:\/[^ >]+)>/;
const LIST_ITEM_END_R = / *\n+$/;
const LIST_LOOKBEHIND_R = /^$|\n *$/;
const LIST_LOOKBEHIND_R = /(?:^|\n)( *)$/;
const CAPTURE_LETTER_AFTER_HYPHEN = /-([a-z])?/gi;
const NP_TABLE_R = /^(.*\|?.*)\n *(\|? *[-:]+ *\|[-| :]*)\n((?:.*\|.*\n)*)\n?/;
const PARAGRAPH_R = /^((?:[^\n]|\n(?! *\n))+)(?:\n *)+\n/;
Expand Down Expand Up @@ -1103,10 +1103,12 @@ export function compiler (markdown, options) {
// lists can be inline, because they might be inside another list,
// in which case we can parse with inline scope, but need to allow
// nested lists inside this inline scope.
const isStartOfLine = LIST_LOOKBEHIND_R.test(prevCapture);
const isStartOfLine = LIST_LOOKBEHIND_R.exec(prevCapture);
const isListBlock = state._list || !state.inline;

if (isStartOfLine && isListBlock) {
source = isStartOfLine[1] + source;

return LIST_R.exec(source);
} else {
return null;
Expand All @@ -1125,12 +1127,12 @@ export function compiler (markdown, options) {

let lastItemWasAParagraph = false;
const itemContent = items.map(function (item, i) {
// We need to see how far indented this item is:
// We need to see how far indented the item is:
const space = LIST_ITEM_PREFIX_R.exec(item)[0].length;

// And then we construct a regex to "unindent" the subsequent
// lines of the items by that amount:
const spaceRegex = new RegExp('^ {1,' + space + '}', 'gm');
const spaceRegex = new RegExp('^ {' + space + '}', 'gm');

// Before processing the item, we need a couple things
const content = item
Expand Down
19 changes: 19 additions & 0 deletions index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,25 @@ describe('markdown-to-jsx', () => {

expect(root.innerHTML).toMatchSnapshot();
});

it('should handle link trees', () => {
render(compiler(`
- [buttermilk](#buttermilk)
- [installation](#installation)
- [usage](#usage)
- [configuration](#configuration)
- [components](#components)
- [\`<Router>\`](#router)
- [\`<RoutingState>\`](#routingstate)
- [\`<Link>\`](#link)
- [utilities](#utilities)
- [\`route(url: String, addNewHistoryEntry: Boolean = true)\`](#routeurl-string-addnewhistoryentry-boolean--true)
- [holistic example](#holistic-example)
- [goals](#goals)
`));

expect(root.innerHTML).toMatchSnapshot();
});
});

describe('GFM task lists', () => {
Expand Down

0 comments on commit 4312e1d

Please sign in to comment.