i *think* this parses notes containing boldfaced text
parent
8b07ef46a2
commit
2e8f4921ce
31
index.js
31
index.js
|
@ -29,12 +29,19 @@ 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 (s === "") { // this is almost always, except for when we are parsing a multi-element note
|
||||||
if (i.type === "text" && i.value.slice(0,3) === " - ") {
|
if (i.type === "text" && i.value.slice(0,3) === " - ") {
|
||||||
// author found
|
// author found
|
||||||
let parenIndex = i.value.indexOf("(");
|
let parenIndex = i.value.indexOf("(");
|
||||||
|
@ -53,22 +60,40 @@ let parseListItem = function (listItem) {
|
||||||
if (i.type === "link") {
|
if (i.type === "link") {
|
||||||
// other links found
|
// other links found
|
||||||
if (entry.otherLinks === undefined) entry.otherLinks = [];
|
if (entry.otherLinks === undefined) entry.otherLinks = [];
|
||||||
entry.otherLinks.push({title: i.children[0].value, url: i.url});
|
entry.otherLinks.push({title: stripParens(i.children[0].value), url: i.url});
|
||||||
// entry.otherLinks = [...entry.otherLinks, {title: i.children[0].value, url: i.url}]; // <-- i wish i could get this syntax to work with arrays
|
// 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 === "text" && i.value.indexOf("(") !== -1) {
|
if (i.type === "text" && i.value.indexOf("(") !== -1 && i.value.indexOf(")") !== -1) {
|
||||||
// notes found (currently assumes no nested parentheses)
|
// notes found (currently assumes no nested parentheses)
|
||||||
if (entry.notes === undefined) entry.notes = [];
|
if (entry.notes === undefined) entry.notes = [];
|
||||||
let leftParen = i.value.indexOf("(");
|
let leftParen = i.value.indexOf("(");
|
||||||
let rightParen = i.value.indexOf(")", leftParen);
|
let rightParen = i.value.indexOf(")", leftParen);
|
||||||
if (rightParen === -1) {
|
if (rightParen === -1) {
|
||||||
// there must be some *emphasis* found
|
// there must be some *emphasis* found
|
||||||
// TODO: this
|
s += i.value.slice(leftParen + 1);
|
||||||
} else {
|
} else {
|
||||||
entry.notes.push(i.value.slice(leftParen + 1, rightParen));
|
entry.notes.push(i.value.slice(leftParen + 1, rightParen));
|
||||||
}
|
}
|
||||||
// also TODO: if theres more than one disjoint set of parens
|
// also TODO: if theres more than one disjoint set of parens
|
||||||
}
|
}
|
||||||
|
} 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 {
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return entry;
|
return entry;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue