Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerska committed Oct 2, 2020
1 parent 432b67a commit 24a9051
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
15 changes: 14 additions & 1 deletion frontend/src/AlgoliasearchNetlify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { AutocompleteWrapper } from './AutocompleteWrapper';
import { Options } from './options';

const defaultOptions: Omit<Options, 'appId' | 'apiKey' | 'indexName'> = {
const defaultOptions: Omit<
Options,
'appId' | 'apiKey' | 'indexName' | 'siteId' | 'branch'
> = {
analytics: true,
autocomplete: {
hitsPerPage: 5,
Expand All @@ -20,11 +23,21 @@ class AlgoliasearchNetlify {
constructor(_options: Options) {
AlgoliasearchNetlify.instances.push(this);

// Temporary
const splitIndexName = (
indexName: string
): { siteId: string; branch: string } => {
const regexp = /^netlify_([0-9a-f-]+)_(.*)_all$/;
const [, siteId, branch] = indexName.match(regexp)!;
return { siteId, branch };
};

// eslint-disable-next-line no-warning-comments
// TODO: add validation
const options = {
...defaultOptions,
..._options,
...(_options.indexName && splitIndexName(_options.indexName)), // Temporary
autocomplete: {
...defaultOptions.autocomplete,
..._options.autocomplete,
Expand Down
13 changes: 10 additions & 3 deletions frontend/src/AutocompleteWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ const SM_WIDTH = 600;
class AutocompleteWrapper {
// All fields are private because they're just here for debugging
private client: SearchClient;
private indexName: string;
private index: SearchIndex;

private $inputs: HTMLInputElement[] = [];
private autocompletes: AutocompleteJs[] = [];

constructor({ appId, apiKey, indexName }: Options) {
constructor({ appId, apiKey, siteId, branch }: Options) {
this.client = this.createClient(appId, apiKey);
this.indexName = indexName;
const indexName = this.computeIndexName(siteId, branch);
this.index = this.client.initIndex(indexName);
}

Expand Down Expand Up @@ -108,6 +107,14 @@ class AutocompleteWrapper {
this.autocompletes = autocompletes;
}

private computeIndexName(siteId: string, branch: string): string {
// Keep in sync with crawler code in /netlify/crawl
const cleanBranch = branch
.replace(/[^\p{L}\p{N}_.-]+/gu, '-')
.replace(/-{2,}/g, '-');
return `netlify_${siteId}_${cleanBranch}_all`;
}

private createClient(appId: string, apiKey: string): SearchClient {
const client = algoliasearch(appId, apiKey);
client.addAlgoliaAgent(`Netlify integration ${version}`);
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ export interface Options {
// Mandatory
appId: string;
apiKey: string;
indexName: string;

// Temporary
indexName?: string;
siteId: string;
branch: string;

// Optional
analytics: boolean;
Expand Down
1 change: 1 addition & 0 deletions public/1.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<title>First test page</title>
<meta name="description" content="First test page description" />
</head>
<body>
<h2>First test page</h2>
Expand Down
1 change: 1 addition & 0 deletions public/2.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<title>Second test page</title>
<meta name="description" content="Second test page description" />
</head>
<body>
<h2>Second test page</h2>
Expand Down
8 changes: 5 additions & 3 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

<head>
<title>Algoliasearch Netlify Test Website</title>
<meta name="description" content="Algolia x Netlify testing website." />

<link rel="icon" href="/favicon.ico" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/necolas/normalize.css@8.0.1/normalize.css" />

Expand Down Expand Up @@ -145,9 +147,9 @@ <h2>Test content</h2>
type="text/javascript"></script>
<script type="text/javascript">
const DEFAULT_SOURCE = `algoliasearchNetlify({
appId: 'FHMV5EW9NC',
apiKey: 'aea9a7c4604b1e1a4d57fd29b2dc0a3f',
indexName: 'test-manual-index',
appId: '4C7VLPQA76',
apiKey: 'a9cfed5acb56143690b612167d89a1b5',
indexName: 'netlify_9209706f-d5b7-46e2-bb88-5d6bedd2823f_feat-plugin-add-siteid-branch-param_all',
debug: true,
});`
.split('\n')
Expand Down

0 comments on commit 24a9051

Please sign in to comment.