Skip to content

Commit d43173f

Browse files
committed
Omit paragraph wrappers from list item text
This updates the style of `list_item` to match the format from https://www.markdownguide.org/basic-syntax/#lists-1 Namely, that `list_item` contents do not have vertical margin due to wrapped `paragraph` tokens. If folks do want this behavior, they can override the style of `*_list_icon` or `*_list_content`. This change has the added benefit of eliminating the need for platform specific `*_list_icon` margins and line heights. Horray! iOS before: <img width="433" alt="list_ios_before" src="https://user-images.githubusercontent.com/456610/89119457-90897600-d463-11ea-80a7-05ea6e86f4a5.png"> iOS after: <img width="441" alt="list_ios_after" src="https://user-images.githubusercontent.com/456610/89119465-94b59380-d463-11ea-8f4c-161fbb43e6a6.png"> Android before: <img width="356" alt="list_android_before" src="https://user-images.githubusercontent.com/456610/89119470-98491a80-d463-11ea-8415-31374584603d.png"> Android after: <img width="357" alt="list_android_after" src="https://user-images.githubusercontent.com/456610/89119474-9c753800-d463-11ea-98d9-07b4e1585b44.png">
1 parent ed1e4bd commit d43173f

File tree

3 files changed

+31
-41
lines changed

3 files changed

+31
-41
lines changed

src/lib/parser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import tokensToAST from './util/tokensToAST';
22
import {stringToTokens} from './util/stringToTokens';
33
import {cleanupTokens} from './util/cleanupTokens';
44
import groupTextTokens from './util/groupTextTokens';
5+
import omitListItemParagraph from './util/omitListItemParagraph';
56

67
/**
78
*
@@ -18,6 +19,7 @@ export default function parser(source, renderer, markdownIt) {
1819
let tokens = stringToTokens(source, markdownIt);
1920
tokens = cleanupTokens(tokens);
2021
tokens = groupTextTokens(tokens);
22+
tokens = omitListItemParagraph(tokens);
2123

2224
const astTree = tokensToAST(tokens);
2325

src/lib/styles.js

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,6 @@ export const styles = {
6666
bullet_list_icon: {
6767
marginLeft: 10,
6868
marginRight: 10,
69-
...Platform.select({
70-
android: {
71-
marginTop: 5,
72-
},
73-
ios: {
74-
marginTop: 0,
75-
},
76-
default: {
77-
marginTop: 0,
78-
},
79-
}),
80-
...Platform.select({
81-
ios: {
82-
lineHeight: 36,
83-
},
84-
android: {
85-
lineHeight: 30,
86-
},
87-
default: {
88-
lineHeight: 36,
89-
},
90-
}),
9169
},
9270
// @pseudo class, does not have a unique render rule
9371
bullet_list_content: {
@@ -98,25 +76,6 @@ export const styles = {
9876
ordered_list_icon: {
9977
marginLeft: 10,
10078
marginRight: 10,
101-
...Platform.select({
102-
android: {
103-
marginTop: 4,
104-
},
105-
default: {
106-
marginTop: 0,
107-
},
108-
}),
109-
...Platform.select({
110-
ios: {
111-
lineHeight: 36,
112-
},
113-
android: {
114-
lineHeight: 30,
115-
},
116-
default: {
117-
lineHeight: 36,
118-
},
119-
}),
12079
},
12180
// @pseudo class, does not have a unique render rule
12281
ordered_list_content: {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export default function omitListItemParagraph(tokens) {
2+
// used to ensure that we remove the correct ending paragraph token
3+
let depth = null;
4+
return tokens.filter((token, index) => {
5+
// update depth if we've already removed a starting paragraph token
6+
if (depth !== null) {
7+
depth = depth + token.nesting;
8+
}
9+
10+
// check for a list_item token followed by paragraph token (to remove)
11+
if (token.type === 'list_item' && token.nesting === 1 && depth === null) {
12+
const next = index + 1 in tokens ? tokens[index + 1] : null;
13+
if (next && next.type === 'paragraph' && next.nesting === 1) {
14+
depth = 0;
15+
return true;
16+
}
17+
} else if (token.type === 'paragraph') {
18+
if (token.nesting === 1 && depth === 1) {
19+
// remove the paragraph token immediately after the list_item token
20+
return false;
21+
} else if (token.nesting === -1 && depth === 0) {
22+
// remove the ending paragraph token; reset depth
23+
depth = null;
24+
return false;
25+
}
26+
}
27+
return true;
28+
});
29+
}

0 commit comments

Comments
 (0)