search will correctly match files based on keyword

pull/3/head
David Tookey 5 years ago
parent 3fd194138d
commit 6b237dfbbb

@ -114,27 +114,24 @@ export class DocumentDirectory {
private static walkCards(d: DirectoryNode): Map<string, object[]> { private static walkCards(d: DirectoryNode): Map<string, object[]> {
let cardsByCategory = new Map<string, object[]>(); let cardsByCategory = new Map<string, object[]>();
for (let i = 0, l = d.children.length; i < l; i++) { for (let child of d.children) {
let child = d.children[i];
if (child instanceof DirectoryNode) { if (child instanceof DirectoryNode) {
let dir = child as DirectoryNode; let dir = child as DirectoryNode;
if (dir.containsDirectory()) { if (dir.containsDirectory()) {
let childDirectories = dir.getDirectories(); let childDirectories = dir.getDirectories();
for (let j = 0, k = childDirectories.length; j < k; j++) { for (let dir of childDirectories) {
let dir = childDirectories[j];
let subCards = DocumentDirectory.walkCards(dir); let subCards = DocumentDirectory.walkCards(dir);
this.mergeMaps(cardsByCategory, subCards); this.mergeMaps(cardsByCategory, subCards);
} }
} }
let sCards = dir.getDocuments(); let sCards = dir.getDocuments();
let category = dir.getCategory(); let category = dir.getCategory();
for (let sCard in sCards) { for (let sCard of sCards) {
let scrd = sCards[sCard];
let cards = cardsByCategory.get(category); let cards = cardsByCategory.get(category);
if (cards === undefined || cards === null) { if (cards === undefined || cards === null) {
cards = []; cards = [];
} }
cards.push(scrd.toCard()); cards.push(sCard.toCard());
cardsByCategory.set(category, cards); cardsByCategory.set(category, cards);
} }
} else { } else {
@ -153,8 +150,7 @@ export class DocumentDirectory {
private static mergeMaps(a: Map<string, Object[]>, b: Map<string, Object[]>) { private static mergeMaps(a: Map<string, Object[]>, b: Map<string, Object[]>) {
let keys = Object.keys(b); let keys = Object.keys(b);
for (let z in keys) { for (let key of keys) {
let key = keys[z];
let sa = a.get(key) let sa = a.get(key)
if (sa === undefined || sa === null) { if (sa === undefined || sa === null) {
sa = []; sa = [];
@ -213,12 +209,12 @@ export class FileNode {
"imagePath": imageName, "imagePath": imageName,
"urlText": this.filePath, "urlText": this.filePath,
"altText": "", "altText": "",
"fileCard": true "fileCard": true,
"open": this.open
} }
let altKeys = Object.keys(altProps); let altKeys = Object.keys(altProps);
for (let kidx in altKeys) { for (let key of altKeys) {
let key = altKeys[kidx];
cardObj[key] = altProps[key] cardObj[key] = altProps[key]
} }
@ -251,9 +247,9 @@ export class DirectoryNode extends FileNode {
let stats = fs.lstatSync(filePath); let stats = fs.lstatSync(filePath);
if (stats.isDirectory()) { if (stats.isDirectory()) {
let contents = fs.readdirSync(filePath); let contents = fs.readdirSync(filePath);
for (let i = 0, l = contents.length; i < l; i++) { for (let fileName of contents) {
if (path.extname(contents[i]) === ".json") continue; if (path.extname(fileName) === ".json") continue;
let childPath = path.join(filePath, contents[i]); let childPath = path.join(filePath, fileName);
let childStats = fs.lstatSync(childPath); let childStats = fs.lstatSync(childPath);
if (childStats.isDirectory()) { if (childStats.isDirectory()) {
this.children.push(new DirectoryNode(childPath, this)); this.children.push(new DirectoryNode(childPath, this));
@ -282,8 +278,7 @@ export class DirectoryNode extends FileNode {
} }
containsDirectory(): Boolean { containsDirectory(): Boolean {
for (let i = 0, l = this.children.length; i < l; i++) { for (let child of this.children) {
let child = this.children[i];
if (child instanceof DirectoryNode) return true; if (child instanceof DirectoryNode) return true;
} }
return false return false

@ -127,8 +127,14 @@ function addToFileMap(map: Map<string, object>, categoryKey: string, card) {
function cardContainsTerm(term: string, card: object): boolean { function cardContainsTerm(term: string, card: object): boolean {
let titleContains = card["title"].toLowerCase().includes(term.toLowerCase()); let titleContains = card["title"].toLowerCase().includes(term.toLowerCase());
let descriptionContains = card["description"].toLowerCase().includes(term.toLowerCase()); let descriptionContains = card["description"].toLowerCase().includes(term.toLowerCase());
let keywordContains = false;
if (card["keywords"]) {
keywordContains = card["keywords"].toLowerCase().includes(term.toLowerCase());
}
//todo: add keywords //todo: add keywords
return titleContains || descriptionContains; return titleContains || descriptionContains || keywordContains;
} }
function highlightCardTerm(term: string, card: object): object { function highlightCardTerm(term: string, card: object): object {

Loading…
Cancel
Save