-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an extension to collect Bloblang samples and make them available …
…to the UI (#85) * Add an extension to collect Bloblang samples and make them available to the UI * Apply suggestions from code review * Add tests * Use playground directory
- Loading branch information
1 parent
720da55
commit cae7eef
Showing
7 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,78 @@ | ||
const yaml = require('js-yaml'); | ||
|
||
module.exports.register = function () { | ||
const logger = this.getLogger('collect-bloblang-samples'); | ||
|
||
this.on('contentClassified', ({ contentCatalog }) => { | ||
const collectExamples = (examples, componentName) => { | ||
const bloblangSamples = []; | ||
const seenTitles = new Set(); | ||
|
||
examples | ||
.filter((example) => example.src.relative.startsWith('playground/')) // Only include files in the 'bloblang' subdirectory | ||
.forEach((example) => { | ||
try { | ||
const content = example.contents.toString('utf8'); | ||
const parsedContent = yaml.load(content); | ||
|
||
if (!parsedContent.title) { | ||
logger.warn(`Skipping example '${example.src.basename}' in '${componentName}': Missing title.`); | ||
return; | ||
} | ||
|
||
if (seenTitles.has(parsedContent.title)) { | ||
logger.warn( | ||
`Duplicate title found: '${parsedContent.title}' in '${example.src.basename}' (${componentName}). Skipping.` | ||
); | ||
return; | ||
} | ||
|
||
if (!parsedContent.input || !parsedContent.mapping) { | ||
logger.warn( | ||
`Skipping example '${example.src.basename}' in '${componentName}': Missing input or mapping.` | ||
); | ||
return; | ||
} | ||
|
||
logger.info(`Loaded example: ${example.src.basename} with title: '${parsedContent.title}'`); | ||
seenTitles.add(parsedContent.title); | ||
|
||
bloblangSamples.push({ filename: example.src.basename, ...parsedContent }); | ||
} catch (error) { | ||
logger.error(`Error processing example '${example.src.basename}' in '${componentName}':`, error); | ||
} | ||
}); | ||
|
||
bloblangSamples.sort((a, b) => a.title.localeCompare(b.title)); | ||
|
||
return bloblangSamples.reduce((acc, sample) => { | ||
acc[sample.filename] = sample; | ||
return acc; | ||
}, {}); | ||
}; | ||
|
||
// Fetch examples from both components | ||
const examples = contentCatalog.findBy({ component: 'redpanda-connect', family: 'example' }); | ||
const previewExamples = contentCatalog.findBy({ component: 'preview', family: 'example' }); | ||
|
||
if (!examples.length) logger.warn(`No examples found in the 'redpanda-connect' component.`); | ||
|
||
// Get components | ||
const connect = contentCatalog.getComponents().find((c) => c.name === 'redpanda-connect'); | ||
const preview = contentCatalog.getComponents().find((c) => c.name === 'preview'); | ||
|
||
if (connect) { | ||
const connectSamples = collectExamples(examples, 'redpanda-connect'); | ||
connect.latest.asciidoc.attributes['page-bloblang-samples'] = JSON.stringify(connectSamples); | ||
logger.debug(`Bloblang samples added to 'redpanda-connect': ${JSON.stringify(connectSamples, null, 2)}`); | ||
} else { | ||
logger.warn(`Component 'redpanda-connect' not found.`); | ||
} | ||
|
||
if (preview) { | ||
const previewSamples = collectExamples(previewExamples, 'preview'); | ||
preview.latest.asciidoc.attributes['page-bloblang-samples'] = JSON.stringify(previewSamples); | ||
logger.debug(`Bloblang samples added to 'preview': ${JSON.stringify(previewSamples, null, 2)}`); | ||
} | ||
}); | ||
}; |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
7 changes: 7 additions & 0 deletions
7
preview/extensions-and-macros/modules/ROOT/examples/playground/hello.yaml
This file contains 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,7 @@ | ||
title: Hello world | ||
input: | | ||
{ | ||
"message": "hello world" | ||
} | ||
mapping: | | ||
root.message = this.message.uppercase() |
This file contains 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