i *think* this parses notes containing boldfaced text
parent
8b07ef46a2
commit
2e8f4921ce
83
index.js
83
index.js
|
@ -29,45 +29,70 @@ const excludes = [
|
||||||
* @return {Object} Returns an Object containing details about the piece of media Exact format TBD.
|
* @return {Object} Returns an Object containing details about the piece of media Exact format TBD.
|
||||||
*/
|
*/
|
||||||
let parseListItem = function (listItem) {
|
let parseListItem = function (listItem) {
|
||||||
|
let stripParens = function (s) {
|
||||||
|
if (s.slice(0,1) === "(" && s.slice(-1) === ")")
|
||||||
|
return s.slice(1,-1);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
let entry = {};
|
let entry = {};
|
||||||
|
let s = ""; // If we need to build up a string over multiple listItem elements
|
||||||
const [link, ...otherStuff] = listItem; // head of listItem = url, the rest is "other stuff"
|
const [link, ...otherStuff] = listItem; // head of listItem = url, the rest is "other stuff"
|
||||||
entry.url = link.url;
|
entry.url = link.url;
|
||||||
entry.title = link.children[0].value;
|
entry.title = link.children[0].value;
|
||||||
// remember to get OTHER STUFF!! remember there may be multiple links!
|
// remember to get OTHER STUFF!! remember there may be multiple links!
|
||||||
for (let i of otherStuff) {
|
for (let i of otherStuff) {
|
||||||
if (i.type === "text" && i.value.slice(0,3) === " - ") {
|
if (s === "") { // this is almost always, except for when we are parsing a multi-element note
|
||||||
// author found
|
if (i.type === "text" && i.value.slice(0,3) === " - ") {
|
||||||
let parenIndex = i.value.indexOf("(");
|
// author found
|
||||||
if (parenIndex === -1) {
|
let parenIndex = i.value.indexOf("(");
|
||||||
entry.author = i.value.slice(3).trim();
|
if (parenIndex === -1) {
|
||||||
|
entry.author = i.value.slice(3).trim();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
entry.author = i.value.slice(3, parenIndex).trim(); // go from " - " until the first "("
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
if (i.type === "emphasis" &&
|
||||||
entry.author = i.value.slice(3, parenIndex).trim(); // go from " - " until the first "("
|
i.children[0].value.slice(0, 1) === "(" && i.children[0].value.slice(-1) === ")") {
|
||||||
|
// access notes found (currently assumes exactly one child, so far this is always the case)
|
||||||
|
entry.accessNotes = i.children[0].value.slice(1, -1);
|
||||||
}
|
}
|
||||||
}
|
if (i.type === "link") {
|
||||||
if (i.type === "emphasis" &&
|
// other links found
|
||||||
i.children[0].value.slice(0, 1) === "(" && i.children[0].value.slice(-1) === ")") {
|
if (entry.otherLinks === undefined) entry.otherLinks = [];
|
||||||
// access notes found (currently assumes exactly one child, so far this is always the case)
|
entry.otherLinks.push({title: stripParens(i.children[0].value), url: i.url});
|
||||||
entry.accessNotes = i.children[0].value.slice(1, -1);
|
// entry.otherLinks = [...entry.otherLinks, {title: i.children[0].value, url: i.url}]; // <-- i wish i could get this syntax to work with arrays
|
||||||
}
|
}
|
||||||
if (i.type === "link") {
|
if (i.type === "text" && i.value.indexOf("(") !== -1 && i.value.indexOf(")") !== -1) {
|
||||||
// other links found
|
// notes found (currently assumes no nested parentheses)
|
||||||
if (entry.otherLinks === undefined) entry.otherLinks = [];
|
if (entry.notes === undefined) entry.notes = [];
|
||||||
entry.otherLinks.push({title: i.children[0].value, url: i.url});
|
let leftParen = i.value.indexOf("(");
|
||||||
// entry.otherLinks = [...entry.otherLinks, {title: i.children[0].value, url: i.url}]; // <-- i wish i could get this syntax to work with arrays
|
let rightParen = i.value.indexOf(")", leftParen);
|
||||||
}
|
if (rightParen === -1) {
|
||||||
if (i.type === "text" && i.value.indexOf("(") !== -1) {
|
// there must be some *emphasis* found
|
||||||
// notes found (currently assumes no nested parentheses)
|
s += i.value.slice(leftParen + 1);
|
||||||
if (entry.notes === undefined) entry.notes = [];
|
} else {
|
||||||
let leftParen = i.value.indexOf("(");
|
entry.notes.push(i.value.slice(leftParen + 1, rightParen));
|
||||||
let rightParen = i.value.indexOf(")", leftParen);
|
}
|
||||||
if (rightParen === -1) {
|
// also TODO: if theres more than one disjoint set of parens
|
||||||
// there must be some *emphasis* found
|
}
|
||||||
// TODO: this
|
} else { // for now we assume that all previous ifs are mutually exclusive with this, may polish later
|
||||||
|
if (i.type === "emphasis") {
|
||||||
|
// this is the emphasis, add it in boldface and move on
|
||||||
|
s += "*" + i.children[0].value + "*";
|
||||||
} else {
|
} else {
|
||||||
entry.notes.push(i.value.slice(leftParen + 1, rightParen));
|
// hopefully this is the end of the note
|
||||||
|
let rightParen = i.value.indexOf(")");
|
||||||
|
if (rightParen === -1) {
|
||||||
|
// we have to go AGAIN
|
||||||
|
s += i.value;
|
||||||
|
} else {
|
||||||
|
// finally, we have reached the end of the note
|
||||||
|
entry.notes.push(s + i.value.slice(0, rightParen));
|
||||||
|
s = "";
|
||||||
|
// TODO: there can be a normal note following a bold-containing note
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// also TODO: if theres more than one disjoint set of parens
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return entry;
|
return entry;
|
||||||
|
|
Loading…
Reference in New Issue