-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractAndTestCodeBlocksPlugin.js
More file actions
195 lines (154 loc) · 5.99 KB
/
Copy pathextractAndTestCodeBlocksPlugin.js
File metadata and controls
195 lines (154 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const regex = /```[\s\S]*?```/gm;
// oramaSearchPlugin.js
const { create, insert, search } = require('@orama/orama');
const { dir } = require('console');
const path = require('path');
const fs = require('fs').promises;
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
import { writeFile } from 'fs';
function parseFrontmatter(content) {
// Match content between --- markers
const frontmatterRegex = /^---\s*\n([\s\S]*?)\n---/;
const match = content.match(frontmatterRegex);
if (!match) return null;
// Parse the yaml-like frontmatter
const frontmatter = {};
const lines = match[1].split('\n');
for (const line of lines) {
const [key, ...valueParts] = line.split(':');
if (key && valueParts.length) {
// Trim whitespace and quotes
const value = valueParts.join(':').trim().replace(/^['"](.*)['"]$/, '$1');
frontmatter[key.trim()] = value;
}
}
return frontmatter;
}
function sanitizeInput(text) {
// Remove characters that could break the script tag
return text.replace(/[\u0000-\u001F\u007F<>]/g, ''); // Removes control characters and angle brackets
}
async function markdownToPlainText(markdown) {
const sanitizedMarkdown = sanitizeInput(markdown); // Sanitize input
const file = await unified()
.use(remarkParse) // Parse the Markdown
.use(remarkStringify) // Convert it back to string (plain text)
.process(sanitizedMarkdown); // Process the sanitized input
return String(file); // Return the plain text output
}
function getSlugFromPath(filePath) {
// Remove file extension
const withoutExtension = filePath.replace(/\.[^/.]+$/, '');
// Handle index files
if (withoutExtension.toLowerCase().endsWith('/index')) {
return withoutExtension.slice(0, -6); // Remove '/index'
}
// Remove date prefix if exists (e.g., 2024-01-01-)
const withoutDate = withoutExtension.replace(/^\d{4}-\d{2}-\d{2}-/, '');
// Convert to URL-friendly format
return withoutDate;
}
function parseSlug(fileContent, filePath) {
try {
// Get frontmatter
const frontmatter = parseFrontmatter(fileContent);
// If frontmatter has slug, use it
if (frontmatter?.slug) {
const slug = frontmatter.slug.trim();
return slug.startsWith('/') ? slug : `/${slug}`;
}
// Otherwise generate from file path
const slugFromPath = getSlugFromPath(filePath);
return slugFromPath.startsWith('/') ? slugFromPath : `/${slugFromPath}`;
} catch (error) {
console.error('Error parsing slug:', error);
return null;
}
}
function extractMarkdownCodeBlocksWithoutMultilineFlag(text, filename, codeCollection) {
const blockStart = '``'; // Simplified to match only the opening code blocks without considering multilines or triples
let currentBlock = '';
let extractedBlocks = [];
const lines = text.split('\n');
for (let line of lines) {
if (line.startsWith(blockStart)) {
if(line.startsWith('````')) line = line.split('````')[1]
if(line.startsWith('```')) line = line.split('```')[1]
// Start a new block or add to the existing one
if (!currentBlock) currentBlock += line + '\n';
else currentBlock += `\n${line}`;
} else if (currentBlock && !line.endsWith('``')) {
// Add non-block lines until we find an end marker
currentBlock += `${line}`;
}
// When encountering the ending triple backticks, add to extracted blocks and reset the block
if (line.endsWith(blockStart)) {
if(currentBlock?.length > 2) extractedBlocks.push(currentBlock);
currentBlock = '';
}
}
codeCollection[filename] = extractedBlocks
return codeCollection
}
module.exports = function oramaSearchPlugin(context, options) {
let db;
return {
name: 'extract-and-test',
async loadContent() {
// Create the Orama database
let codeCollection = {}
// Get the docs directory path
const docsDir = path.join(context.siteDir, 'docs');
// Read all markdown files in the docs directory
const files = await fs.readdir(docsDir);
async function getAllMarkdownFiles(dir) {
const entries = await fs.readdir(dir, { withFileTypes: true });
const files = await Promise.all(entries.map(async (entry) => {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
return getAllMarkdownFiles(fullPath);
}
return entry.name.endsWith('.md') ? [fullPath] : [];
}));
return files.flat();
}
const markdownFiles = await getAllMarkdownFiles(docsDir);
//const markdownFiles = files.filter(file => file.endsWith('.md'));
//
// Process each markdown file
for (const file of markdownFiles) {
const content = await fs.readFile(file, 'utf-8');
const divideContent = (content) => {
const frontmatterRegex = /^---\s*\n([\s\S]*?)\n---/;
const match = content.match(frontmatterRegex);
if (!match) {
return {
frontmatter: '',
markdown: content.trim()
};
}
const frontmatter = match[1];
const markdown = content.slice(match[0].length).trim();
return {
frontmatter,
markdown
};
};
const { frontmatter, markdown } = divideContent(content);
extractMarkdownCodeBlocksWithoutMultilineFlag(markdown, file, codeCollection)
//
}
const outputFilePath = path.join(context.siteDir, 'allCodeExamples.json');
await fs.writeFile(outputFilePath, JSON.stringify(codeCollection), 'utf-8');
return
},
async contentLoaded({ content, actions }) {
// You can perform additional actions here if needed
},
async postBuild({ siteConfig, routesPaths, outDir }) {
// You can perform actions after the build is complete
},
};
};