forked from mientjan/react-native-markdown-renderer
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from abirchall/style_lists
This is good, sorry it took so long to merge!
- Loading branch information
Showing
3 changed files
with
31 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export default function omitListItemParagraph(tokens) { | ||
// used to ensure that we remove the correct ending paragraph token | ||
let depth = null; | ||
return tokens.filter((token, index) => { | ||
// update depth if we've already removed a starting paragraph token | ||
if (depth !== null) { | ||
depth = depth + token.nesting; | ||
} | ||
|
||
// check for a list_item token followed by paragraph token (to remove) | ||
if (token.type === 'list_item' && token.nesting === 1 && depth === null) { | ||
const next = index + 1 in tokens ? tokens[index + 1] : null; | ||
if (next && next.type === 'paragraph' && next.nesting === 1) { | ||
depth = 0; | ||
return true; | ||
} | ||
} else if (token.type === 'paragraph') { | ||
if (token.nesting === 1 && depth === 1) { | ||
// remove the paragraph token immediately after the list_item token | ||
return false; | ||
} else if (token.nesting === -1 && depth === 0) { | ||
// remove the ending paragraph token; reset depth | ||
depth = null; | ||
return false; | ||
} | ||
} | ||
return true; | ||
}); | ||
} |