Files
khoj/src/interface/web/index.html
2022-07-28 20:56:37 +04:00

205 lines
6.2 KiB
HTML

<html>
<head>
<meta charset="utf-8">
<title>Khoj</title>
</head>
<script type="text/javascript" src="static/assets/org.js"></script>
<script type="text/javascript" src="static/assets/markdown-it.js"></script>
<script>
function render_image(item) {
return `
<a href="${item.entry}" class="image-link">
<img id=${item.score} src="${item.entry}?${Math.random()}"
title="Effective Score: ${item.score}, Meta: ${item.metadata_score}, Image: ${item.image_score}"
class="image">
</a>`
}
function render_org(query, data) {
var orgCode = `Query: ${query}\n` + data.map(function (item) {
return `${item.entry}`
}).join("\n")
var orgParser = new Org.Parser();
var orgDocument = orgParser.parse(orgCode);
var orgHTMLDocument = orgDocument.convert(Org.ConverterHTML);
return orgHTMLDocument.toString();
}
function render_markdown(query, data) {
var md = window.markdownit();
return md.render(`# Query: ${query}\n` + data.map(function (item) {
return `${item.entry}`
}).join("\n"));
}
function render_json(data, query, type) {
if (type === "markdown") {
return render_markdown(query, data);
} else if (type === "org") {
return render_org(query, data);
} else if (type === "image") {
return data.map(render_image).join('');
} else {
return `<pre id="json">${JSON.stringify(data, null, 2)}</pre>`;
}
}
function search(rerank=false) {
query = document.getElementById("query").value;
type = document.getElementById("type").value;
console.log(query, type);
url = type === "image"
? `/search?q=${query}&t=${type}&n=6`
: `/search?q=${query}&t=${type}&n=6&r=${rerank}`;
fetch(url)
.then(response => response.json())
.then(data => {
console.log(data);
document.getElementById("results").innerHTML =
`<div id=results-${type}>`
+ render_json(data, query, type)
+ `</div>`;
});
}
function regenerate() {
type = document.getElementById("type").value;
fetch(`/regenerate?t=${type}`)
.then(response => response.json())
.then(data => {
console.log(data);
document.getElementById("results").innerHTML =
render_json(data);
});
}
function incremental_search(event) {
type = document.getElementById("type").value;
if (event.key === 'Enter') {
search(rerank=true);
}
// Limit incremental search to text types
else if (type !== "image") {
search(rerank=false);
}
}
</script>
<body>
<h1>Khoj</h1>
<!--Add Text Box To Enter Query, Trigger Incremental Search OnChange -->
<input type="text" id="query" onkeyup=incremental_search(event) placeholder="What is the meaning of life?">
<!--Add Dropdown to Select Query Type.
Query Types can be: org, ledger, image, music, markdown.
Set Default type to image-->
<select id="type">
<option value="image">Image</option>
<option value="org">Org</option>
<option value="markdown">Markdown</option>
<option value="ledger">Ledger</option>
<option value="music">Music</option>
</select>
<!--Add Button To Regenerate -->
<button id="regenerate" onclick="regenerate()">Regenerate</button>
<!-- Section to Render Results -->
<div id="results"></div>
</body>
<style>
@media only screen and (max-width: 600px) {
body {
display: grid;
grid-template-columns: 1fr;
}
body > * {
grid-column: 1;
}
}
@media only screen and (min-width: 600px) {
body {
display: grid;
grid-template-columns: 1fr min(70vw, 100%) 1fr;
padding-top: 60vw;
}
body > * {
grid-column: 2;
}
}
body {
padding: 0px;
margin: 0px;
background: #eee;
color: #888;
text-align: center;
font-family: roboto, karma, segoe ui, sans-serif;
font-size: 20px;
font-weight: 300;
line-height: 1.5em;
}
body > * {
padding: 10px;
margin: 10px;
}
h1 {
font-weight: 200;
color: #888;
}
button {
border-radius: 5px;
border: 1px solid #ccc;
}
#results-image {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.image-link {
place-self: center;
}
.image {
width: 20vw;
border-radius: 10px;
border: 1px solid #ccc;
}
#json {
white-space: pre-wrap;
}
#results-markdown {
text-align: left;
white-space: pre-line;
}
#results-org {
text-align: left;
white-space: pre-line;
}
span.task-status {
color: white;
padding: 3.5px 3.5px 0;
margin-right: 5px;
border-radius: 5px;
background-color: #ed6f00;
}
span.task-status.todo {
background-color: #048ba8
}
span.task-status.done {
background-color: #06a77d;
}
span.task-tag {
color: white;
padding: 3.5px 3.5px 0;
margin-right: 5px;
border-radius: 5px;
background-color: #bbb;
}
</style>
</html>