handle markdown deviations

pull/3/head
nrfq 2021-10-14 12:37:21 -04:00
parent 549ea63edd
commit c13cb51980
2 changed files with 27 additions and 11 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ node_modules/
!README.md !README.md
package-lock.json package-lock.json
*.txt *.txt
*.json

View File

@ -15,14 +15,13 @@ const remark = require('remark');
* *
* @return {Object} Returns an AST as an object, based on the format defined in remark-parse. * @return {Object} Returns an AST as an object, based on the format defined in remark-parse.
*/ */
let getAST = function(filename, outputFile="tree.txt"){ let getAST = function(filename, outputFile="tree.json"){
//import test markdown file //import test markdown file
let mk = fs.readFileSync(path.join(__dirname, filename), "utf8"); let mk = fs.readFileSync(path.join(__dirname, filename), "utf8");
// parse into AST with remark // parse into AST with remark
let tree = remark.parse(mk); let tree = remark.parse(mk);
// write to file for human readibility // write to file for human readibility
fs.writeFile(outputFile, JSON.stringify(tree, null, 3), function(err) { fs.writeFileSync(outputFile, JSON.stringify(tree, null, 3), function(err) {
if (err) { if (err) {
console.log(err); console.log(err);
} }
@ -44,7 +43,7 @@ let getAST = function(filename, outputFile="tree.txt"){
* *
* @return {Object} Returns a JSON object, exact format still in progress. * @return {Object} Returns a JSON object, exact format still in progress.
*/ */
let exportJSON = function(children, outputFile="root.txt"){ let exportJSON = function(children, outputFile="root.json"){
// This will be the JSON to export // This will be the JSON to export
let rootJSON = { let rootJSON = {
type: 'root', type: 'root',
@ -74,21 +73,29 @@ let parseListItem = function(listItem){
} }
let main = function(){ let main = function(){
let tree = getAST("test.md"); let tree = getAST("test_de.md");
let children = []; // This will go into root object later let children = []; // This will go into root object later
let currentDepth = 3; let currentDepth = 3;
// start from 4 to skip index and meta-lists // find index to skip index and meta-lists
// probably could be done better, review later
// TODO: IMPLEMENT FORMAT FOR META-LISTS // TODO: IMPLEMENT FORMAT FOR META-LISTS
for(let i = 4; i < tree.length; i++){ let i=0, count = 0;
for(i; i < tree.length; i++){
if(tree[i].type=='heading' && tree[i].depth=='3')
count++;
if(count == 2)
break;
}
for(i; i < tree.length; i++){
if(tree[i].type == "heading"){ // If any kind of section heading if(tree[i].type == "heading"){ // If any kind of section heading
if(tree[i].depth == 3){ // Make a new child of the root if(tree[i].depth == 3){ // Make a new child of the root
currentDepth = tree[i].depth; currentDepth = 3;
let newGroup = {group: tree[i].children[0].value, entries: [], subsections: []}; let newGroup = {group: tree[i].children[0].value, entries: [], subsections: []};
children.push(newGroup); children.push(newGroup);
} }
else if(tree[i].depth == 4){ // Make a subsection of last group else if(tree[i].depth == 4){ // Make a subsection of last group
currentDepth = tree[i].depth; currentDepth = 4;
let newSubsection = {group: tree[i].children[0].value, entries: []} let newSubsection = {group: tree[i].children[0].value, entries: []}
children[children.length-1].subsections.push(newSubsection); // Push subsection to most recently added element. children[children.length-1].subsections.push(newSubsection); // Push subsection to most recently added element.
} }
@ -97,8 +104,16 @@ let main = function(){
for(let j = 0; j < tree[i].children.length; j++){ //for each listItem for(let j = 0; j < tree[i].children.length; j++){ //for each listItem
let content = tree[i].children[j].children[0].children; //This starts at "type: link" for most entries. Needs parsing tho let content = tree[i].children[j].children[0].children; //This starts at "type: link" for most entries. Needs parsing tho
if(currentDepth == 3){ if(currentDepth == 3){
try{
children[children.length-1].entries.push(parseListItem(content)); children[children.length-1].entries.push(parseListItem(content));
} }
catch(e){
console.log(children[children.length-1]);
// console.log(tree[i]);
console.log("error");
return 1;
}
}
else if(currentDepth == 4){ else if(currentDepth == 4){
let lastChild = children.length-1; // Index of last added h3 Group let lastChild = children.length-1; // Index of last added h3 Group
let lastSubSec = children[lastChild].subsections.length-1; // Index of last added h4 group let lastSubSec = children[lastChild].subsections.length-1; // Index of last added h4 group