Skip to content

Commit

Permalink
Merge pull request #681 from ResearchHub/elastic-search-ui
Browse files Browse the repository at this point in the history
Elastic Search UI
  • Loading branch information
joshslee authored Dec 16, 2020
2 parents 8214217 + f22036c commit 276d67a
Show file tree
Hide file tree
Showing 7 changed files with 759 additions and 765 deletions.
2 changes: 1 addition & 1 deletion components/Navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import InviteToHubModal from "../components/modal/InviteToHubModal";
import LoginModal from "../components/modal/LoginModal";
import PermissionNotificationWrapper from "./PermissionNotificationWrapper";
import Reputation from "./Reputation";
import Search from "./Search";
import Search from "./Search/Search";
import TransactionModal from "../components/modal/TransactionModal";
import UploadPaperModal from "../components/modal/UploadPaperModal";
import Notification from "./Notifications/Notification";
Expand Down
90 changes: 90 additions & 0 deletions components/Search/Highlight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import React, { Fragment } from "react";
import { StyleSheet, css } from "aphrodite";

// Config
import { doesNotExist } from "~/config/utils";

const Highlight = (props) => {
const { result, attribute } = props;
const { highlight } = result.meta;

const highlightSpan = formatHighlightByAttribute();

function formatHighlightByAttribute() {
switch (attribute) {
case "authors":
return transformAuthors();
case "abstract":
case "first_name":
case "last_name":
case "title":
default:
return parseHighlight();
}
}

function parseHighlight(_text) {
if (
doesNotExist(_text) &&
(doesNotExist(highlight) || doesNotExist(highlight[attribute]))
)
return result[attribute];

const text = _text ? _text : highlight[attribute][0];
const parts = text.split(/(<em>[^<]+<\/em>)/);
const parsedString = parts.map((part) => {
if (part.includes("<em>")) {
let replaced = part.replace("<em>", "");
replaced = replaced.replace("</em>", "");
return <span className={css(styles.highlight)}>{replaced}</span>;
}
return <span>{part}</span>;
});

return parsedString;
}

function transformAuthors() {
const authors =
highlight && highlight.authors ? highlight.authors : result.authors;

return (
<div className={css(styles.authors) + " clamp1"}>
{"by "}
{authors.map((author, i) => {
let isLast = i === authors.length - 1;
let result = parseHighlight(author);

if (isLast) {
return result;
}

return (
<Fragment>
{result}
{", "}
</Fragment>
);
})}
</div>
);
}

return highlightSpan ? highlightSpan : null;
};

const styles = StyleSheet.create({
authors: {
fontSize: 12,
fontWeight: 400,
color: "#918F9B",
marginTop: 5,
},
highlight: {
backgroundColor: "#f6e653",
padding: "2px 1px 2px 1px",
fontStyle: "italic",
},
});

export default Highlight;
Loading

0 comments on commit 276d67a

Please sign in to comment.