-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(search): Add search using Algolia
- Loading branch information
1 parent
b28c75f
commit 21b07d8
Showing
24 changed files
with
319 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import algoliasearch from "algoliasearch" | ||
import { algolia } from "../config" | ||
import cheerio from "cheerio" | ||
|
||
const log = require("debug")("dnh:agolia") | ||
const { appId, indexName } = algolia | ||
const adminKey = process.env.ALGOLIA_ADMIN_KEY | ||
|
||
const breakHTMLIntoNodes = (html) => { | ||
const $ = cheerio.load(html) | ||
const result = [] | ||
|
||
$("p").each((i, elem) => { | ||
result.push({ | ||
tag_name: "p", | ||
selector: i + 1, | ||
content: $(elem).text(), | ||
}) | ||
}) | ||
|
||
return result | ||
} | ||
export default async function sendToAlgolia(posts) { | ||
|
||
const data = posts.map(({ content, meta }) => { | ||
const nodes = breakHTMLIntoNodes(content) | ||
const baseObject = { | ||
...meta, | ||
} | ||
|
||
return nodes.map((node) => ({ | ||
objectID: `${ baseObject.id }_${ node.tag_name }_${ node.selector }`, | ||
...baseObject, | ||
...node, | ||
})) | ||
}) | ||
// Flatten array | ||
.reduce((result, node) => result.concat(node), []) | ||
|
||
const client = algoliasearch(appId, adminKey) | ||
const index = client.initIndex(indexName) | ||
index.saveObjects(data) | ||
.then(() => { | ||
log("Sent data to Agolia") | ||
}) | ||
.catch((error) => { | ||
log(error) | ||
log("Error while sending data to Agolia") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { writeFile } from "fs-promise" | ||
import { join } from "path" | ||
|
||
const template = (meta, content) => ( | ||
`---json | ||
${ JSON.stringify(meta)} | ||
--- | ||
${ content } | ||
` | ||
) | ||
|
||
export default async function saveToFile( | ||
fileData, | ||
distPath, | ||
) { | ||
const { meta, content } = fileData | ||
const filePath = join(distPath, fileData.meta.id.toString() + ".md") | ||
const fileContent = template(meta, content) | ||
|
||
await writeFile(filePath, fileContent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React, { PropTypes } from "react" | ||
import Link from "statinamic/lib/Link" | ||
import Time from "../Time" | ||
import styles from "./styles.css" | ||
|
||
const PostEntry = ({ post }) => (( | ||
<div className={ styles.postEntry }> | ||
<Time | ||
format="MMMM YYYY" | ||
time={ post.date } | ||
className={ styles.time } | ||
/> | ||
<span style={ { padding: "5px" } } /> | ||
{ | ||
post.tags && post.tags.map((tag) => ( | ||
<span key={ tag } className={ styles.tag }>{ tag }</span> | ||
)) | ||
} | ||
<h2> | ||
<Link | ||
style={ { borderBottom: "none" } } | ||
to={ post.__url } | ||
> | ||
<span dangerouslySetInnerHTML={ { __html: post.title } } /> | ||
</Link> | ||
</h2> | ||
<p dangerouslySetInnerHTML={ { __html: post.description } } /> | ||
<Link | ||
className={ styles.readmore } | ||
to={ post.__url } | ||
> | ||
{ "Read" } | ||
</Link> | ||
</div> | ||
)) | ||
|
||
PostEntry.propTypes = { | ||
post: PropTypes.object.isRequired, | ||
} | ||
|
||
export default PostEntry |
Oops, something went wrong.