Skip to content

Commit

Permalink
Added a browsing mechanism similar to the original's Wikipedia one
Browse files Browse the repository at this point in the history
  • Loading branch information
aledipa committed Jun 17, 2023
1 parent a4a5546 commit 9a5bd16
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
27 changes: 25 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions dist/views/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="css/style.css">
<link rel="icon" href="img/wikipedia_freepik.png" type="image/png">
<title>miniWikipedia - <%= title %> </title>
</head>
<body>
<!-- NAVBAR -->
<nav class="w-screen h-16 flex pt-2 pl-1 fixed top-0 z-10 bg-[#F9F9F9] grid grid-rows-2 grid-flow-col gap-4">
<!-- TOP-LEFT LOGO -->
<p class="flex justify-start items-end row-start-2 row-span-2">
<a class="ml-2 hover:text-gray-500" href="/">
<img src="img/miniWikipedia_horizontal.svg" class="h-12 mb-2">
</a>
</p>

<!-- input bar -->
<div class="inputBar w-full h-1/5 flex justify-center items-center mt-12 row-start-1 row-end-4">
<form action="/result" method="GET" class="w-full flex justify-end ">
<input type="text" name="search" id="search"
class="w-7/12 h-10 pl-5 text-left text-xl helvetica font-thin outline-0 border-b border-l border-black bg-[#F9F9F9]"
placeholder="Search..." required autocomplete="off">
<div class="suggestions">
<ul></ul>
</div>
<button id="submit" class="w-12 h-24 hover:opacity-70">
<img src="img/search_catalinfertu.png" class="h-8 hover:opacity-70 mb-16 ml-2">
</button>
</form>
</div>
</nav>


<!-- Result -->
<div class="mt-40 h-screen">
<p class="ml-24 georgia font-extrabold text-3xl"> <%= title %> </p>
<p class="ml-24 helvetica font-thin text-s text-gray-400"><i> <%= description %> </i></p>
<br>
<!-- bulleted list of titles -->
<!-- TODO: Make the lines below prettier -->
<div class="ml-24 georgia font-thin text-xl text-gray-800 w-7/12"><u><a href="<%= links[1] %>"> <%= search_results[1].title %> </a></u> </div>
<div class="ml-24 georgia font-thin text-xl text-gray-800 w-7/12"><u><a href="<%= links[2] %>"> <%= search_results[2].title %> </a></u> </div>
<div class="ml-24 georgia font-thin text-xl text-gray-800 w-7/12"><u><a href="<%= links[3] %>"> <%= search_results[3].title %> </a></u> </div>
<div class="ml-24 georgia font-thin text-xl text-gray-800 w-7/12"><u><a href="<%= links[4] %>"> <%= search_results[4].title %> </a></u> </div>
<div class="ml-24 georgia font-thin text-xl text-gray-800 w-7/12"><u><a href="<%= links[5] %>"> <%= search_results[5].title %> </a></u> </div>
<div class="ml-24 georgia font-thin text-xl text-gray-800 w-7/12"><u><a href="<%= links[6] %>"> <%= search_results[6].title %> </a></u> </div>
<div class="ml-24 georgia font-thin text-xl text-gray-800 w-7/12"><u><a href="<%= links[7] %>"> <%= search_results[7].title %> </a></u> </div>
<div class="ml-24 georgia font-thin text-xl text-gray-800 w-7/12"><u><a href="<%= links[8] %>"> <%= search_results[8].title %> </a></u> </div>
<div class="ml-24 georgia font-thin text-xl text-gray-800 w-7/12"><u><a href="<%= links[9] %>"> <%= search_results[9].title %> </a></u> </div>
</div>
</body>
</html>
30 changes: 28 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,24 @@ app.get('/result', (req, res) => {
try {
req.query.search = setPhraseCapitalFirstLetters(req.query.search);
const summary = await wiki.summary(req.query.search, {autoSuggest: false});
if (summary.extract.includes("refer to:")) {
throw new Error("No specific summary found");
}
res.render(__dirname + '/views/result.html', {title:summary.title, description:summary.description, summary:summary.extract});
//Response of type @wikiSummary - contains the intro and the main image
} catch (error) {
const summary = await wiki.summary("HTTP 404", {autoSuggest: false});
res.render(__dirname + '/views/result.html', {title:summary.title, description:summary.description, summary:summary.extract});
const search_results = await wiki.search(req.query.search, {suggestion: true, limit: 10});
if (search_results.results.length > 0) {
var links:Array<string> = [];
for (let i=0; i<search_results.results.length; i++) {
links.push(formatSpaces(linkFromTitle(search_results.results[i].title), '_'));
}
res.render(__dirname + '/views/search.html', {title:req.query.search, description:"Topics referred to by the same term", search_results:search_results.results, links:links});
// console.log("Search result link 1: " + linkFromTitle(search_results.results[0].title));
} else {
const summary = await wiki.summary("HTTP 404", {autoSuggest: false});
res.render(__dirname + '/views/result.html', {title:summary.title, description:summary.description, summary:summary.extract});
}
//=> Typeof wikiError
}
})();
Expand Down Expand Up @@ -73,3 +86,16 @@ function setPhraseCapitalFirstLetters(phrase: string) {
});
return capitalizedWords.join(' ');
}

// Replaces spaces with underscores
function formatSpaces(phrase: string, replacement: string) {
let words = phrase.split(' ');
return words.join(replacement);
}


// Creates the link of the result option
function linkFromTitle(title: string) {
return "/result?search=" + title;
}

0 comments on commit 9a5bd16

Please sign in to comment.