-
Notifications
You must be signed in to change notification settings - Fork 97
Extract snippet preprocessor #935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+112
−1
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| // --- CONFIGURATION --- | ||
| // The directories to scan for snippets | ||
| const DOCS_DIRS = ['./docs', './unversioned']; | ||
| // --------------------- | ||
|
|
||
| const snippetRegistry = new Map(); | ||
| let isIndexed = false; | ||
|
|
||
| // Helper: Recursively find all .md/.mdx files | ||
| const getAllFiles = (dirPath, arrayOfFiles = []) => { | ||
| if (!fs.existsSync(dirPath)) return arrayOfFiles; | ||
|
|
||
| const files = fs.readdirSync(dirPath); | ||
| files.forEach((file) => { | ||
| const fullPath = path.join(dirPath, file); | ||
| if (fs.statSync(fullPath).isDirectory()) { | ||
| getAllFiles(fullPath, arrayOfFiles); | ||
| } else if (file.endsWith('.md') || file.endsWith('.mdx')) { | ||
| arrayOfFiles.push(fullPath); | ||
| } | ||
| }); | ||
| return arrayOfFiles; | ||
| }; | ||
|
|
||
| // Helper: Extract Doc ID from Frontmatter | ||
| const getDocId = (content, filename) => { | ||
| const idMatch = content.match(/^---\s+[\s\S]*?\nid:\s*(.*?)\s*[\n\r]/m); | ||
| if (idMatch && idMatch[1]) { | ||
| return idMatch[1].replace(/['"]/g, '').trim(); | ||
| } | ||
| return filename; | ||
| }; | ||
|
|
||
| // --- CORE LOGIC --- | ||
| const buildIndex = () => { | ||
| if (isIndexed) return; | ||
| console.log('[ExtractPreprocessor] ⚡ Indexing snippets via Regex...'); | ||
|
|
||
| const allFiles = []; | ||
| DOCS_DIRS.forEach(dir => getAllFiles(path.resolve(process.cwd(), dir), allFiles)); | ||
|
|
||
| let count = 0; | ||
|
|
||
| // Regex to find: <div data-extract="ID"> CONTENT </div> | ||
| // We use [\s\S]*? to match content across multiple lines (lazy match) | ||
| const extractRegex = /<div\s+data-extract=["']([^"']+)["'][^>]*>([\s\S]*?)<\/div>/g; | ||
|
|
||
| allFiles.forEach(filePath => { | ||
| try { | ||
| const content = fs.readFileSync(filePath, 'utf8'); | ||
| const filename = path.basename(filePath, path.extname(filePath)); | ||
| const docId = getDocId(content, filename); | ||
|
|
||
| let match; | ||
| // Loop through all matches in the file | ||
| while ((match = extractRegex.exec(content)) !== null) { | ||
| let [fullTag, extractId, snippetContent] = match; | ||
|
|
||
| // Clean up the content (optional: trim leading/trailing newlines) | ||
| snippetContent = snippetContent.replace(/^\n+|\n+$/g, ''); | ||
|
|
||
| // Generate Key: "docId:snippetId" | ||
| // If the ID already has a colon, assume user provided full ID | ||
| const key = extractId.includes(':') ? extractId : `${docId}:${extractId}`; | ||
|
|
||
| snippetRegistry.set(key, snippetContent); | ||
| console.log(`[ExtractPreprocessor] ⚡ Indexed snippet: ${key}`); | ||
| count++; | ||
| } | ||
| } catch (e) { | ||
| console.warn(`[ExtractPreprocessor] Failed to read ${filePath}`); | ||
| } | ||
| }); | ||
|
|
||
| isIndexed = true; | ||
| console.log(`[ExtractPreprocessor] ⚡ Indexed ${count} snippets.`); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now remove all divs that have been indexed |
||
| }; | ||
|
|
||
| // This function is called by Docusaurus for EVERY markdown file | ||
| const preprocessor = ({ filePath, fileContent }) => { | ||
| // 1. Ensure Index exists (runs once) | ||
| buildIndex(); | ||
|
|
||
| // 2. Regex to find: <div data-extract-copy="ID" /> | ||
| // Matches <div data-extract-copy="xyz"></div> OR <div data-extract-copy="xyz" /> | ||
| const copyRegex = /<div\s+data-extract-copy=["']([^"']+)["']\s*\/?>\s*(?:<\/div>)?/g; | ||
|
|
||
| // 3. Replace with content | ||
| return fileContent.replace(copyRegex, (match, requestedId) => { | ||
| if (snippetRegistry.has(requestedId)) { | ||
| // Return the stored snippet content | ||
| return snippetRegistry.get(requestedId); | ||
| } else { | ||
| console.error(`[ExtractPreprocessor] ❌ Snippet not found: "${requestedId}" in ${path.basename(filePath)}`); | ||
| // Return an error message in the UI so you see it | ||
| return `> **Error: Snippet "${requestedId}" not found.**`; | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| module.exports = preprocessor; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ This page lists all possible health check issues, their trigger conditions, and | |
| - 🌟 **Bonus**: Optimization advice and upper-tier recommendations with strong return on value *(coming in future Stack Health iterations)* | ||
|
|
||
| ## Health Check Issues Overview | ||
| <div data-extract="stackhealth_issues_list"> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the div once the extracts have been indexed |
||
|
|
||
| | Issue | Criticality | Summary | Resolution | | ||
| |-------|---------------|---------|------------| | ||
|
|
@@ -30,6 +31,7 @@ This page lists all possible health check issues, their trigger conditions, and | |
| | **Security Engine Offline** | 🔥 Critical | Security Engine has not reported to Console for 24+ hours | [Troubleshooting](/u/troubleshooting/issue_se_offline) | | ||
| | **Security Engine Too Many Alerts** | ⚠️ High | More than 250,000 alerts in 6 hours | [Troubleshooting](/u/troubleshooting/issue_se_too_many_alerts) | | ||
|
|
||
| </div> | ||
| ## Issue Dependencies | ||
|
|
||
| Some issues are related and share common root causes: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try using a custom tag, if it's removed docusauus shjould not be bothered