Skip to content

Instantly share code, notes, and snippets.

@ramyak-mehra
Last active April 25, 2021 00:22
Show Gist options
  • Save ramyak-mehra/ec9d89c7a58dff5131ff90c10e3dab69 to your computer and use it in GitHub Desktop.
Save ramyak-mehra/ec9d89c7a58dff5131ff90c10e3dab69 to your computer and use it in GitHub Desktop.
Parse a markdown and divide it into sections
import 'package:markdown/markdown.dart';
import 'dart:io' as io;
final _structuralHeaderTags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
class Section {
int level;
Node titleNode;
List<Node> contentNodes;
List<Section> children;
Section parent;
void addContentNodes(Node nodes) {
contentNodes = List.from(contentNodes)..add(nodes);
}
Section({level, titleNode}) {
this.level = level;
this.titleNode = titleNode;
contentNodes = [];
children = [];
}
}
void parser(List<Node> nodes) {
var finalSectionList = <Section>[];
Section currentSection;
for (final node in nodes) {
final nodeTag = node is Element ? node.tag : null;
final isHeading =
nodeTag != null && _structuralHeaderTags.contains(nodeTag);
var section = Section(titleNode: node);
if (isHeading) {
section.level = _structuralHeaderTags.indexOf(nodeTag);
}
if (currentSection == null) {
currentSection = section;
finalSectionList.add(section);
continue;
}
if (isHeading) {
final currentLevel = _structuralHeaderTags.indexOf(nodeTag);
var previousLevel = currentSection.level;
if (currentLevel > previousLevel) {
currentSection.children.add(section);
section.parent = currentSection;
currentSection = section;
continue;
} else if (currentLevel < previousLevel) {
while (currentLevel < previousLevel) {
currentSection = currentSection.parent;
previousLevel = currentSection.level;
}
if (currentSection.parent != null) {
currentSection = currentSection.parent;
section.parent = currentSection;
currentSection.children.add(section);
currentSection = section;
} else {
finalSectionList.add(section);
currentSection = section;
}
} else {
if (currentSection.parent != null) {
currentSection = currentSection.parent;
section.parent = currentSection;
currentSection.children.add(section);
currentSection = section;
} else {
finalSectionList.add(section);
currentSection = section;
}
}
} else {
currentSection.contentNodes.add(node);
}
}
printEverything(finalSectionList);
}
void printEverything(List<Section> sections) {
sections.forEach((element) {
print(element.level);
print(element.titleNode.textContent);
print('--------content nodes----------');
element.contentNodes.forEach((e) {
print(e.textContent);
});
print('--------children----------');
printEverything(element.children);
print('--------end----------');
});
}
void main(List<String> args) {
final pwd = io.Directory.current.path;
final nodes = getNodes(pwd + '/bin/readme2.md');
parser(nodes);
}
List<Node> getNodes(String filePath) {
var file = io.File(filePath);
var document = Document();
var markdown = file.readAsStringSync();
var lines = markdown.replaceAll('\r\n', '\n').split('\n');
return document.parseLines(lines);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment