diff --git a/documentation/plugins/code-snippets/codeSnippets.js b/documentation/plugins/code-snippets/codeSnippets.js index 11edeb8b21..4d6f16e26d 100644 --- a/documentation/plugins/code-snippets/codeSnippets.js +++ b/documentation/plugins/code-snippets/codeSnippets.js @@ -1,7 +1,7 @@ const fs = require('fs'); const path = require('path'); -const codeBlockRegex = /\/\/<(?\/)?code-block:(?[a-zA-Z0-9_]*)>$/; +const codeBockRegex = /\/\/<(?\/)?code-block:(?[a-zA-Z0-9_]*)>$/; module.exports = function() { const snippets = {}; @@ -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; @@ -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') + }; } } @@ -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); } }); } @@ -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)}`; -}; \ No newline at end of file +};