Skip to content

Instantly share code, notes, and snippets.

@rileyhawk1417
Created September 15, 2023 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rileyhawk1417/8e8ed1e4da288765d3ea66a1796d451e to your computer and use it in GitHub Desktop.
Save rileyhawk1417/8e8ed1e4da288765d3ea66a1796d451e to your computer and use it in GitHub Desktop.
Bulleted List node suggestion for handling children
//NOTE: Helper functions
String childNode(String node) {
String tmpWord = '';
final spacesRegex = RegExp(r'(\s+)- ');
const spacesPerLevel = 2;
int level = 1;
if (node.startsWith(' ') && node.contains('- ')) {
final spaces = spacesRegex.firstMatch(node)!.group(1)!.length;
level = (spaces / spacesPerLevel).ceil();
tmpWord = node.substring(level);
}
return tmpWord;
}
bool checkChild(String child) {
if (child.startsWith(' ')) {
return true;
}
return false;
}
bool checkParent(String parent) {
if (parent.startsWith('- ') &&
(parent.contains('- ', 0) || parent.contains('- ', 1))) {
return true;
}
return false;
}
String firstNode(String node) {
String parentList = '';
if (node.startsWith('- ') &&
(node.contains('- ', 0) || node.contains('- ', 1))) {
final String firstWord = node.substring(2);
parentList = node.substring(2);
return firstWord;
}
return parentList;
// return null;
}
return bulletedListNode(
//NOTE: Just prints double the parent
attributes: checkParent(firstNode(line))
? {
'delta': decoder.convert(firstNode(line)).toJson(),
}
: {},
//NOTE: Child node works fine although
children: checkChild(childNode(line))
? [
bulletedListNode(
attributes: {
'delta':
decoder.convert(childNode(line).substring(2)).toJson()
},
),
]
: [],
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment