Add support for custom code extensions in markdown #2032
+67
−14
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.
This is my first pass at adding support for custom code extensions, these will allow you to add support for your favourite language or tool in side the markdown.
Inside the
observablehq.config.ts
file you can add handlers like so:Then in a markdown file you can simply say:
And it will result in
The example is pretty stupid, but this could easily allow clojure compilation to javascript or SQL for BigQuery etc.
The result of the function call returns valid JavaScript, that will be parsed by the
parseJavaScript
. This change simply inserts itself in thegetLiveSource
. To be honest the logic could easily be pulled one level up tomakeFenceRenderer
however this allows one to clearly see how this supports overriding the built in handlers if desired.A later refactor could be to convert the built in code handlers into a default CodeExtensions object. Again I was trying to keep the changes to an absolute minimum to increase chance of acceptance (but totally open to feedback).
Probably the most important thing in this PR is the name of the config option
codeExtensions
, other options I thought about might becode
,codeBlock
,fenceCodeBlocks
,languageBlocks
,languageHandlers
,tag
etc. Please give feedback and I will update all references to the same name.I also tried to follow the same code style of each file, again to maximise chance of acceptance.
Last I added a very simple test for the above uppercase examples, and ran all the tests, linter and typechecker before commiting.