This repository has been archived by the owner on Apr 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added fragment indexing (merged feature branch)
- Loading branch information
Showing
6 changed files
with
638 additions
and
72 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
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,43 @@ | ||
const algoliaSearch = require('algoliasearch'), | ||
config = require('../../../../current/core/server/config'), | ||
algoliaSettings = config.get('algolia'); | ||
|
||
|
||
const indexFactory = () => { | ||
let _fragments = []; | ||
let index; | ||
|
||
return { | ||
connect: () => { | ||
if(algoliaSettings && algoliaSettings.active === true) { | ||
if(algoliaSettings.applicationID && algoliaSettings.apiKey && algoliaSettings.index) { | ||
let client = algoliaSearch(algoliaSettings.applicationID, algoliaSettings.apiKey); | ||
index = client.initIndex(algoliaSettings.index); | ||
return true; | ||
} else { | ||
// TODO better error output on frontend | ||
console.log('Please check your Algolia for a missing configuration option: applicationID, apiKey, index.'); | ||
return false; | ||
} | ||
} else { | ||
console.log('Algolia indexing deactivated.') | ||
} | ||
}, | ||
addFragment: (fragment) => { | ||
if(fragment.content !== undefined || fragment.heading !== undefined) { | ||
_fragments.push(fragment); | ||
} | ||
}, | ||
hasFragments: () => { | ||
return _fragments.length > 0; | ||
}, | ||
add: (post) => { | ||
return index.addObjects(_fragments); | ||
}, | ||
delete: (post) => { | ||
return index.deleteByQuery(post.attributes.uuid, {restrictSearchableAttributes: 'post_uuid'}); | ||
}, | ||
} | ||
} | ||
|
||
module.exports = indexFactory; |
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,56 @@ | ||
const parse = require('markdown-to-ast').parse, | ||
removeMd = require('remove-markdown'); | ||
slug = require('slug'); | ||
|
||
const parserFactory = () => { | ||
return { | ||
|
||
// Returns true if any fragments have been successfully parsed | ||
parse: (post, index) => { | ||
let fragment = {}, | ||
headingCount = 0; | ||
|
||
const markdown = JSON.parse(post.attributes.mobiledoc).cards[0][1].markdown, | ||
astChildren = parse(markdown).children; | ||
|
||
if(astChildren.length !== 0) { | ||
// Set first hypothetical headless fragment attributes. | ||
if(astChildren[0].type !== 'Header') { | ||
fragment.id = post.attributes.slug; | ||
fragment.importance = 0; // we give a higher importance to the intro | ||
// aka the first headless fragment. | ||
fragment.post_uuid = post.attributes.uuid; | ||
fragment.post_title = post.attributes.title; | ||
fragment.post_published_at = post.attributes.published_at; | ||
} | ||
|
||
astChildren.forEach(function(element){ | ||
if(element.type === 'Header') { | ||
// Send previous fragment | ||
index.addFragment(fragment); | ||
|
||
fragment = {}; | ||
headingCount ++; | ||
fragment.heading = element.children[0].value; | ||
fragment.id = post.attributes.slug + '#card-markdown--' + slug(fragment.heading) + '--' + headingCount; | ||
fragment.importance = element.depth; | ||
fragment.post_uuid = post.attributes.uuid; | ||
fragment.post_title = post.attributes.title; | ||
fragment.post_published_at = post.attributes.published_at; | ||
} else { | ||
if(fragment.content === undefined) fragment.content = ''; | ||
fragment.content += removeMd(element.raw) + '\n'; | ||
} | ||
}); | ||
|
||
// Saving the last fragment (as saving only happens as a new heading | ||
// is found). This also takes care of heading-less articles. | ||
index.addFragment(fragment); | ||
} | ||
|
||
return index.hasFragments(); | ||
} | ||
} | ||
} | ||
|
||
module.exports = parserFactory; |
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
Oops, something went wrong.