|
| 1 | +/* ALGOLIA SCRAPER |
| 2 | + * This script searches for relevant content to index in docsDir. |
| 3 | + * Before running it make sure you have the file algolia-apikey.js |
| 4 | + * containing your ALGOLIA_API_KEY and make sure you have it on gitignore. |
| 5 | + * |
| 6 | + * example: node algolia.js |
| 7 | + * |
| 8 | + * IMPORTANT NOTE: |
| 9 | + * We have been accepted into the algolia program for FOSS projects. |
| 10 | + * So this scraper is not really necessary anymore unless that changes. |
| 11 | + * */ |
| 12 | + |
| 13 | +const fs = require('fs'); |
| 14 | +const path = require('path'); |
| 15 | +const algoliasearch = require('algoliasearch'); |
| 16 | +const cheerio = require('cheerio'); |
| 17 | +const { ALGOLIA_API_KEY } = require('./.algolia-apikey'); |
| 18 | + |
| 19 | +// MAKE SURE YOUR TOKEN IS CORRECT |
| 20 | +// Initialize Algolia client with your credentials |
| 21 | +const appId = 'S1DLXCNPKJ'; // Your Algolia Application ID |
| 22 | +const apiKey = ALGOLIA_API_KEY; // Get API key from apikey.js |
| 23 | +const client = algoliasearch(appId, apiKey); |
| 24 | +const index = client.initIndex('normalnvim'); |
| 25 | + |
| 26 | +// Directory containing your Docusaurus build content |
| 27 | +const docsDir = path.join(__dirname, 'build', 'docs'); |
| 28 | + |
| 29 | +// Toggle DEBUG_MODE to true or false |
| 30 | +const DEBUG_MODE = false; |
| 31 | + |
| 32 | +// Debug print function |
| 33 | +function debugPrint(message, ...optionalParams) { |
| 34 | + if (DEBUG_MODE) { |
| 35 | + console.log(message, ...optionalParams); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Function to calculate relevancy score |
| 40 | +function calculateRelevancyScore(content) { |
| 41 | + // Basic example: higher scores for longer content |
| 42 | + // You can enhance this function with more complex logic |
| 43 | + const lengthScore = content.length / 1000; // Normalize length |
| 44 | + return Math.min(lengthScore, 1); // Ensure score is between 0 and 1 |
| 45 | +} |
| 46 | + |
| 47 | +// Recursively traverse directories and process files |
| 48 | +function extractDocsFromDir(dir) { |
| 49 | + const records = []; |
| 50 | + |
| 51 | + function readDirRecursive(currentDir) { |
| 52 | + const files = fs.readdirSync(currentDir); |
| 53 | + |
| 54 | + files.forEach((file) => { |
| 55 | + const filePath = path.join(currentDir, file); |
| 56 | + const stats = fs.statSync(filePath); |
| 57 | + |
| 58 | + if (stats.isDirectory()) { |
| 59 | + // Recursively process subdirectories |
| 60 | + readDirRecursive(filePath); |
| 61 | + } else if (stats.isFile() && file.endsWith('.html')) { |
| 62 | + // Process HTML files |
| 63 | + debugPrint(`Processing file: ${filePath}`); |
| 64 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 65 | + debugPrint(`Content of ${file}:`, content.slice(0, 100)); // Show first 100 characters |
| 66 | + |
| 67 | + // Parse and create records using cheerio |
| 68 | + const $ = cheerio.load(content); |
| 69 | + const title = $('h1').first().text() || $('title').text() || ''; |
| 70 | + const bodyContent = $('main').text() || $('body').text() || ''; |
| 71 | + |
| 72 | + const relevancyScore = calculateRelevancyScore(bodyContent); |
| 73 | + |
| 74 | + const record = { |
| 75 | + objectID: path.relative(docsDir, filePath), |
| 76 | + title: title, |
| 77 | + content: bodyContent, |
| 78 | + url: `https://normalnvim.github.io/docs/${path.relative(docsDir, filePath).replace(/\index.html$/, '')}`, |
| 79 | + relevancy: relevancyScore, |
| 80 | + hierarchy: { |
| 81 | + lvl0: title || '', |
| 82 | + lvl1: '', // Adjust if your content has specific sub-levels |
| 83 | + lvl2: '', // Adjust if needed |
| 84 | + lvl3: '', // Adjust if needed |
| 85 | + lvl4: '', // Adjust if needed |
| 86 | + lvl5: '', // Adjust if needed |
| 87 | + }, |
| 88 | + }; |
| 89 | + |
| 90 | + debugPrint('Generated record:', record); |
| 91 | + records.push(record); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + readDirRecursive(dir); |
| 97 | + return records; |
| 98 | +} |
| 99 | + |
| 100 | +async function uploadRecords() { |
| 101 | + const records = extractDocsFromDir(docsDir); |
| 102 | + try { |
| 103 | + const { objectIDs } = await index.saveObjects(records); |
| 104 | + console.log('Algolia - Index updated successfully:', objectIDs); |
| 105 | + } catch (error) { |
| 106 | + console.error('Error uploading documents:', error); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +uploadRecords(); |
| 111 | + |
0 commit comments