Added types to searcg indexing helpers

This commit is contained in:
Piotr Janosz
2019-07-29 14:36:51 +02:00
committed by fabioberger
parent 11e689156e
commit 1e44bcb7c9

View File

@@ -7,9 +7,9 @@ const filter = require('unist-util-filter');
const { selectAll } = require('unist-util-select');
const extractMdxMeta = require('extract-mdx-metadata');
function processContentTree(tree: any, url: any, meta: any, index: any): void {
function processContentTree(tree: Node[], url: string, meta: Meta, index: any): void {
const filteredTree = filter(tree, () => {
return (node: any) => node.type === 'heading' || node.type === 'paragraph';
return (node: Node) => node.type === 'heading' || node.type === 'paragraph';
});
const textNodes = selectAll('text', filteredTree);
@@ -22,15 +22,15 @@ function processContentTree(tree: any, url: any, meta: any, index: any): void {
}
}
export function setIndexSettings(index: any, settings: any): void {
index.setSettings(settings, (err: any) => {
export function setIndexSettings(index: any, settings: Settings): void {
index.setSettings(settings, (err: string) => {
if (err) {
throw Error(`Error: ${err}`);
}
});
}
function pushObjectsToAlgolia(index: any, content: any): void {
function pushObjectsToAlgolia(index: any, content: Content): void {
index
.saveObjects(content)
.then(({ objectIDs }: { objectIDs: string[] }) =>
@@ -43,12 +43,12 @@ function pushObjectsToAlgolia(index: any, content: any): void {
});
}
function getContent(meta: any, url: string, formattedTextNodes: string[]): any {
function getContent(meta: Meta, url: string, formattedTextNodes: FormattedNode[]): any {
// META SHAPE TOOLS // const { description, difficulty, id, isCommunity, tags, title, type } = meta;
// META SHAPE GUIDES // const { description, difficulty, id, tags, title, topics } = meta;
const content: any = [];
const content: Content[] = [];
formattedTextNodes.forEach((node: any, index: any) => {
formattedTextNodes.forEach((node: FormattedNode, index: number) => {
content.push({
...meta,
url,
@@ -60,14 +60,14 @@ function getContent(meta: any, url: string, formattedTextNodes: string[]): any {
return content;
}
function formatTextNodes(textNodes: any): string[] {
const formattedTextNodes: any = []; // array structure: [ { line: [LINE_NUMBER], textContent: [MERGED_TEXT_VALUE] } ]
function formatTextNodes(textNodes: Node[]): FormattedNode[] {
const formattedTextNodes: FormattedNode[] = []; // array structure: [ { line: [LINE_NUMBER], textContent: [MERGED_TEXT_VALUE] } ]
textNodes.map((textNode: any) => {
textNodes.map((textNode: Node) => {
const { position, value } = textNode;
const { line } = position.start; // Line at which textnode starts (and for paragraphs, headings, ends).
const nodeIndex = formattedTextNodes.findIndex((node: any) => node.line === line);
const nodeIndex = formattedTextNodes.findIndex((node: FormattedNode) => node.line === line);
const isIndexPresent = nodeIndex > -1;
if (isIndexPresent) {
@@ -80,7 +80,7 @@ function formatTextNodes(textNodes: any): string[] {
return formattedTextNodes;
}
async function processMdxAsync(index: any, dirName: any, fileName: any): Promise<void> {
async function processMdxAsync(index: any, dirName: string, fileName: string): Promise<void> {
const filePath = `${dirName}/${fileName}`;
const { name } = path.parse(filePath); // Name without file extension
const url = `/docs/${dirName}/${name}`;
@@ -91,15 +91,80 @@ async function processMdxAsync(index: any, dirName: any, fileName: any): Promise
await remark()
.use(mdx)
.use(() => (tree: any) => processContentTree(tree, url, meta, index))
.use(() => (tree: Node[]) => processContentTree(tree, url, meta, index))
.process(file);
}
export async function indexFiles(index: any, dirName: string): Promise<void> {
fs.readdir(dirName, async (err: string, items: any) => {
// @ts-ignore
fs.readdir(dirName, async (err: string, items: string[]) => {
for (const fileName of items) {
await processMdxAsync(index, dirName, fileName);
}
});
}
interface Meta {
id: number | string;
title: string;
description: string;
difficulty: 'Beginner' | 'Intermediate' | 'Advanced';
isCommunity?: boolean;
isFeatured?: boolean;
tags: string[];
topics: string[];
type: string;
}
interface Content extends Meta {
url: string;
textContent: string;
objectID: string;
}
interface Settings {
distinct: boolean;
attributeForDistinct: string;
attributesForFaceting: string[];
attributesToSnippet: string[];
searchableAttributes: string[];
snippetEllipsisText: string;
}
interface FormattedNode {
line: number;
textContent: string;
}
// Syntactic units in unist syntax trees are called nodes.
interface Node {
type: string;
children?: Node[];
data?: Data;
depth?: number;
lang?: string;
ordered?: boolean;
position?: Position;
spread?: boolean;
value?: string;
}
// Location of a node in a source file.
interface Position {
start: Point; // Place of the first character of the parsed source region.
end: Point; // Place of the first character after the parsed source region.
indent: number[]; // Start column at each index (plus start line) in the source region
}
// One place in a source file.
interface Point {
line: number; // Line in a source file (1-indexed integer).
column: number; // Column in a source file (1-indexed integer).
offset: number; // Character in a source file (0-indexed integer).
}
// Information associated by the ecosystem with the node.
// Space is guaranteed to never be specified by unist or specifications
// implementing unist.
interface Data {
[key: string]: unknown;
}