Skip to content

Commit

Permalink
[Docs] Revert some changes
Browse files Browse the repository at this point in the history
This reverts commit 79979b4.
  • Loading branch information
Marten-Mrfc committed Sep 4, 2024
1 parent 881cc56 commit ee9545b
Showing 1 changed file with 21 additions and 50 deletions.
71 changes: 21 additions & 50 deletions documentation/plugins/code-snippets/codeSnippets.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs');
const path = require('path');

const codeBlockRegex = /\/\/<(?<closing>\/)?code-block:(?<tag>[a-zA-Z0-9_]*)>$/;
const codeBockRegex = /\/\/<(?<closing>\/)?code-block:(?<tag>[a-zA-Z0-9_]*)>$/;

module.exports = function() {
const snippets = {};
Expand All @@ -10,48 +10,26 @@ module.exports = function() {
const content = fs.readFileSync(filePath, 'utf8');
const lines = content.split('\n');
const blocks = {};
const blockStartLines = {};

lines.forEach((line, index) => {
const match = line.match(codeBlockRegex);
const match = line.match(codeBockRegex);
if (match) {
const { closing, tag } = match.groups;
if (closing) {
const code = blocks[tag];
if (!code) {
// Handle the case where a closing tag does not have an opening tag
snippets[tag] = {
file: relativeFilePath,
content: "Not found", // Temporary fix so the docs can be build...
startLine: "N/A",
endLine: index + 1
};
} else {
// Close the block and save the snippet
blocks[tag] = null;
snippets[tag] = {
file: relativeFilePath,
content: code.join('\n'),
startLine: blockStartLines[tag] + 1,
endLine: index + 1
};
throw new Error(`Code block not closed: ${tag} (${relativeFilePath}:${index + 1})`);
}
} else {
if (blocks[tag]) {
// Handle the case where an opening tag already exists
snippets[tag] = {
file: relativeFilePath,
content: "Not found", // Temporary fix so the docs can be build...
startLine: blockStartLines[tag] + 1,
endLine: index + 1
};
}
// Start a new block
blocks[tag] = [];
blockStartLines[tag] = index;
blocks[tag] = null;
snippets[tag] = {
file: relativeFilePath,
content: code.join('\n')
};
return;
}

blocks[tag] = [];
} else {
// Add lines to the current blocks
for (const tag in blocks) {
const code = blocks[tag];
if (!code) continue;
Expand All @@ -63,14 +41,11 @@ module.exports = function() {
// Handle any unclosed blocks
for (const tag in blocks) {
const code = blocks[tag];
if (code) {
snippets[tag] = {
file: relativeFilePath,
content: "Not found", // Temporary fix so the docs can be build...
startLine: blockStartLines[tag] + 1,
endLine: "N/A"
};
}
if (!code) continue;
snippets[tag] = {
file: relativeFilePath,
content: code.join('\n')
};
}
}

Expand All @@ -83,13 +58,7 @@ module.exports = function() {
if (stat.isDirectory()) {
traverseDirectory(filePath, baseDir);
} else if (path.extname(filePath) === '.kt') {
try {
processFile(filePath, relativeFilePath);
} catch (error) {
// Improve error handling by showing which file failed
console.error(`Error processing file: ${relativeFilePath}`);
throw error;
}
processFile(filePath, relativeFilePath);
}
});
}
Expand All @@ -98,11 +67,13 @@ module.exports = function() {
const adaptersDir = path.resolve(__dirname, '../../../extensions/_DocsExtension');
traverseDirectory(adaptersDir, adaptersDir);

// console.log(`Exporting ${Object.keys(snippets).length} snippets: ${Object.keys(snippets)}`);

this.cacheable(false);

const code = `${JSON.stringify(snippets, null, 2)}`;

fs.writeFileSync(path.resolve(__dirname, 'snippets.json'), code);
fs.writeSync(fs.openSync(path.resolve(__dirname, 'snippets.json'), 'w'), code);

return `export default ${JSON.stringify(snippets, null, 2)}`;
};
};

0 comments on commit ee9545b

Please sign in to comment.