Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function findNotes(grade, notes, words) {
if (grade !== 'KF') return null;
const documents = notes.map(note => {
return {
id: note.id,
text: note.text
};
});
const index = lunr(function() {
this.ref('id');
this.field('text');
this.metadataWhitelist = ['position'];
documents.forEach(doc => this.add(doc));
});
// window.index = index;
const results = _.flatMap(words, word => index.search(word));
// console.log('results', results);
// return JSON.stringify(results);
const matches = _.flatMap(results, result => {
const note = notes.filter(note => note.id === parseInt(result.ref, 10))[0]; // need a better way :)
if (!note) {
// console.log('no note for result:', results);
const ENV = process.env.NODE_ENV;
const debug = debugLib('DocsService');
const indexDb = getSearchIndexPath();
marked.setOptions({
highlight: (code) => {
return highlight.highlightAuto(code).value;
}
});
// Generate a hash of valid api routes, from the /configs/apis.js file
let cache = {};
let documents = {};
// setup lunr index
const index = lunr(function () {
debug('Creating lunr index');
this.ref('id');
this.field('title', { boost: 10 });
this.field('description', { boost: 5 });
this.field('body');
this.field('permalink');
});
/**
* Generic function to call GitHub's 'repos' API
* https://developer.github.com/v3/repos/
*
* @function fetchGitHubReposApi
* @param {Object} params
* @param {String} [params.repo=yahoo/fluxible] Repository
* @param {String} params.type Type of data to fetch
constructor(props) {
super(props)
// init client-side search indexing
this._documents = {}
this._index = lunr(function () {
this.ref('path')
this.field('title', {boost: 10})
this.field('text')
this.pipeline.remove(lunr.stopWordFilter)
})
this.state = {query: ''}
}
buildIndex(documents) {
return lunr(function() {
this.ref('id');
this.field('body');
this.metadataWhitelist = ['position'];
documents.forEach(function(doc) {
this.add(doc);
}, this);
});
}
function populateIndex(data) {
var index = lunr(function(){
this.field('title', { boost: 10 });
this.field('content');
this.field('url');
this.ref('id');
var lunr_index = this
data.forEach(function(item) {
lunr_index.add(item);
});
});
return index;
}
function createIndex () {
return lunr(function () {
this.ref('ref')
this.field('title', {boost: 3, extractor: titleExtractor})
this.field('doc')
this.metadataWhitelist = ['position']
this.pipeline.remove(lunr.stopWordFilter)
searchNodes.forEach(function (doc) {
this.add(doc)
}, this)
})
}
function createLunrIndex(options) {
let index = options.index;
return lunr(function() {
Object.keys(index).forEach((key) => {
if (key === 'ref') {
this.ref(index[key])
} else {
this.field(key, index[key])
}
})
});
}
function Search({subfolder, documents, next}) {
const idx = lunr(function() {
this.ref('path') //eslint-disable-line
this.field('title')//eslint-disable-line
this.field('content')//eslint-disable-line
this.field('keywords')//eslint-disable-line
for (const doc of Object.values(documents)) {
this.add(doc) //eslint-disable-line
}
})
const [results, setResults] = useState([])
const onChange = e => {
if (e.target) {
setResults(idx.search(`${e.target.value}*`))
}
}
doSearch(query) {
const contentDir = patch_content_dir(path.normalize(this.config.content_dir));
const files = glob.sync(contentDir + '**/*.md');
const idx = lunr(function() {
this.field('title', { boost: 10 });
this.field('body');
});
files.forEach(filePath => {
try {
const shortPath = filePath.replace(contentDir, '').trim();
const file = fs.readFileSync(filePath);
const meta = this.processMeta(file.toString('utf-8'));
idx.add({
id : shortPath,
title : meta.title ? meta.title : this.slugToTitle(shortPath),
body : file.toString('utf-8')
});
.then((rawData) => {
const data = JSON.parse(rawData);
const index = lunr(function() {
this.field('name', { boost: 10 });
this.field('tags', { boost: 15 });
this.field('description');
this.ref('id');
});
data.forEach((item, id) => index.add({ ...item, id }));
this.setState({ data, index });
this.search('color');
});
}