r/twinegames • u/VegetarianDisneyland • 7h ago
Harlowe 3 Search Bar - Returning Set Passages (passage links not working)?
I'm using Twine in browser, Harlowe 3 story format. I'm new to coding and a beginner with Twine. I'm not sure that Harlowe's javascript can suport what I'm asking, in which case I'll just work around it. It's just for a small scale experiment for my friends anyway.
I'm trying to emulate a libary search function, where players can input searches - for examples, a name or topic, and return 3/4 "articles" (passages). What I really want is for the search results to be passage links, using [[]] format, but when I add it to the code, it just renders as plain text, not a passage link. I found a way around it using url links, but that means my "articles" have to be hosted outside twine, which isn't ideal.
Basically, I'd like to know if it's possible to amend/update the code to support the passage link format.
I have the below search bar in html: <input type='text' name='search-box'></input>
<span id='output'>
with the following in the javascript area (I'm aware that Harlowe doesn't support much javascript):
window.termList = window.termList || [];
window.SearchTerm = window.SearchTerm || function (h, descr, kw) {
'use strict';
if (this instanceof SearchTerm) {
this.handle = h;
this.description = descr;
this.keywords = kw;
} else {
return new SearchTerm(h, descr, kw, dd);
}
};
SearchTerm.add = function (h, descr, kw) {
termList.push(new SearchTerm(h, descr, kw));
};
SearchTerm.prototype = {
compare : function (stringArray) {
var term = this;
var matches = (function () {
var ret = 0;
stringArray.forEach( function (str) {
if (term.keywords.includes(str)) {
ret++;
}
});
return ret;
}());
return matches;
},
constructor : window.SearchTerm
};
$(document).on('change', 'input[name=search-box]', function (e) {
var string = $(this).val(),
matches = [], i, n = -1, highest = 0, content;
string = string.replace(/[^A-Za-z0-9\s]/, ' ');
string = string.replace(/[\s\s+]/, ' ');
string = string.trim();
string = string.split(' ');
termList.forEach( function (term) {
matches.push(term.compare(string));
});
for (i = 0; i < matches.length; i++) {
if (matches[i] > n) {
n = matches[i];
highest = i;
}
}
if (n <= 0) {
content = '';
} else {
content = termList[highest].handle + '<p></p> ' + termList[highest].description;
}
$('#output')
.empty()
.append(content);
});
// add your search terms here...
SearchTerm.add('(2) search results...', '<a href="https://drive.google.com/usp=drive_link">book!</a><p>[[Voice 2]]</p><p></p><a href="https://-wd83MTBZpC/view?usp=drive_link">book!</a>', ['win', 'winning']);
SearchTerm.add('hi','first, stop winning', ['lose', 'losing']);
SearchTerm.add('How to cheese things', 'first, eat winning', ['cheese',])
....
I'd like this bit SearchTerm.add('hi','first, stop winning', ['lose', 'losing']); to say this
SearchTerm.add('[[page 2]]','[[page 5]]', ['lose', 'losing']);
and for the [[page 2]] result to be an active link to that passage.
Any help would be great!




