|
| 1 | +// Minimal engine to test class-based engine override |
| 2 | +// Claims {python.foo} blocks with priority 2 (higher than Jupyter's 1) |
| 3 | + |
| 4 | +let quarto; |
| 5 | + |
| 6 | +const fooEngineDiscovery = { |
| 7 | + init: (quartoAPI) => { |
| 8 | + quarto = quartoAPI; |
| 9 | + }, |
| 10 | + |
| 11 | + name: "foo", |
| 12 | + defaultExt: ".qmd", |
| 13 | + defaultYaml: () => ["engine: foo"], |
| 14 | + defaultContent: () => ["# Foo Engine Document"], |
| 15 | + validExtensions: () => [".qmd"], |
| 16 | + |
| 17 | + claimsFile: (_file, _ext) => false, |
| 18 | + |
| 19 | + claimsLanguage: (language, firstClass) => { |
| 20 | + // Claim python.foo with priority 2 (overrides Jupyter's 1) |
| 21 | + if (language === "python" && firstClass === "foo") { |
| 22 | + return 2; |
| 23 | + } |
| 24 | + return 0; |
| 25 | + }, |
| 26 | + |
| 27 | + canFreeze: false, |
| 28 | + generatesFigures: false, |
| 29 | + |
| 30 | + launch: (context) => { |
| 31 | + return { |
| 32 | + name: "foo", |
| 33 | + canFreeze: false, |
| 34 | + |
| 35 | + markdownForFile: async (file) => { |
| 36 | + return quarto.mappedString.fromFile(file); |
| 37 | + }, |
| 38 | + |
| 39 | + target: async (file, _quiet, markdown) => { |
| 40 | + if (!markdown) { |
| 41 | + markdown = quarto.mappedString.fromFile(file); |
| 42 | + } |
| 43 | + const metadata = quarto.markdownRegex.extractYaml(markdown.value); |
| 44 | + return { |
| 45 | + source: file, |
| 46 | + input: file, |
| 47 | + markdown, |
| 48 | + metadata, |
| 49 | + }; |
| 50 | + }, |
| 51 | + |
| 52 | + partitionedMarkdown: async (file) => { |
| 53 | + const markdown = quarto.mappedString.fromFile(file); |
| 54 | + return quarto.markdownRegex.partition(markdown.value); |
| 55 | + }, |
| 56 | + |
| 57 | + execute: async (options) => { |
| 58 | + // Replace python.foo code blocks with a marker div showing we processed them |
| 59 | + let markdown = options.target.markdown.value; |
| 60 | + |
| 61 | + // Find and replace {python.foo} blocks with our marker output |
| 62 | + const codeBlockRegex = /```\{python\.foo[^}]*\}\n([\s\S]*?)```/g; |
| 63 | + markdown = markdown.replace(codeBlockRegex, (match, code) => { |
| 64 | + return ` |
| 65 | +::: {#foo-engine-marker .foo-engine-output} |
| 66 | +**FOO ENGINE PROCESSED THIS BLOCK** |
| 67 | +
|
| 68 | +Original code: |
| 69 | +\`\`\`python |
| 70 | +${code.trim()} |
| 71 | +\`\`\` |
| 72 | +::: |
| 73 | +`; |
| 74 | + }); |
| 75 | + |
| 76 | + return { |
| 77 | + markdown: markdown, |
| 78 | + supporting: [], |
| 79 | + filters: [], |
| 80 | + }; |
| 81 | + }, |
| 82 | + |
| 83 | + dependencies: async (_options) => { |
| 84 | + return { includes: {} }; |
| 85 | + }, |
| 86 | + |
| 87 | + postprocess: async (_options) => {}, |
| 88 | + }; |
| 89 | + }, |
| 90 | +}; |
| 91 | + |
| 92 | +export default fooEngineDiscovery; |
0 commit comments