Package: datasette-render-markdown
datasette-render-markdownDatasetteplugin for rendering Markdown.InstallationInstall this plugin in the same environment as Datasette to enable this new functionality:datasetteinstalldatasette-render-markdownUsageYou can explicitly list the columns you would like to treat as Markdown usingplugin configurationin ametadata.jsonfile.Add a"datasette-render-markdown"configuration block and use a"columns"key to list the columns you would like to treat as Markdown values:{"plugins":{"datasette-render-markdown":{"columns":["body"]}}}This will cause anybodycolumn in any table to be treated as markdown and safely rendered usingPython-Markdown. The resulting HTML is then run throughBleachto avoid the risk of XSS security problems.Save this tometadata.jsonand run Datasette with the--metadataflag to load this configuration:$ datasette serve mydata.db --metadata metadata.jsonThe configuration block can be used at the top level, or it can be applied just to specific databases or tables. Here's how to apply it to just theentriestable in thenews.dbdatabase:{"databases":{"news":{"tables":{"entries":{"plugins":{"datasette-render-markdown":{"columns":["body"]}}}}}}}And here's how to apply it to everybodycolumn in every table in thenews.dbdatabase:{"databases":{"news":{"plugins":{"datasette-render-markdown":{"columns":["body"]}}}}}Columns that match a naming conventionThis plugin can also render markdown in any columns that match a specific naming convention.By default, columns that have a name ending in_markdownwill be rendered.You can try this out using the following query:select'# Hello there* This is a list* of items[And a link](https://github.com/simonw/datasette-render-markdown).'asdemo_markdownYou can configure a different list of wildcard patterns using the"patterns"configuration key. Here's how to render columns that end in either_markdownor_md:{"plugins":{"datasette-render-markdown":{"patterns":["*_markdown","*_md"]}}}To disable wildcard column matching entirely, set"patterns": []in your plugin metadata configuration.Markdown extensionsThePython-Markdown librarythat powers this plugin supports extensions, bothbundledandthird-party. These can be used to enable additional Markdown features such astable support.You can configure support for extensions using the"extensions"key in your plugin metadata configuration.Since extensions may introduce new HTML tags, you will also need to add those tags to the list of tags that are allowed by theBleachsanitizer. You can do that using the"extra_tags"key, and you can allow-list additional HTML attributes using"extra_attrs". Seethe Bleach documentationfor more information on this.Here's how to enable support forMarkdown tables:{"plugins":{"datasette-render-markdown":{"extensions":["tables"],"extra_tags":["table","thead","tr","th","td","tbody"]}}}GitHub-Flavored MarkdownEnablingGitHub-Flavored Markdown(useful for if you are working with data imported from GitHub usinggithub-to-sqlite) is a little more complicated.First, you will need to install thepy-gfmpackage:$ pip install py-gfmNote thatpy-gfmhasa bugthat causes it to pin toMarkdown<3.0- so if you are using it you should install itbeforeinstallingdatasette-render-markdownto ensure you get a compatibly version of that dependency.Now you can configure it like this. Note that the extension name ismdx_gfm:GithubFlavoredMarkdownExtensionand you need to allow-list several extra HTML tags and attributes:{"plugins":{"datasette-render-markdown":{"extra_tags":["hr","br","details","summary","input"],"extra_attrs":{"input":["type","disabled","checked"],},"extensions":["mdx_gfm:GithubFlavoredMarkdownExtension"]}}}The<input type="" checked disabled>attributes are needed to support rendering checkboxes in issue descriptions.Markdown in templatesThe plugin introduces a new template tag:{% markdown %}...{% endmarkdown %}- which can be used to render Markdown in your Jinja templates.{%markdown%}# This will be rendered as markdown{%endmarkdown%}You can use attributes on the{% markdown %}tag to enable extensions and allow-list additional tags and attributes:{%markdownextensions="tables"extra_tags="table thead tr th td tbody"extra_attrs="p:id,class a:name,href"%}## Markdown table

First Header  | Second Header
------------- | -------------
Content Cell  | Content Cell
Content Cell  | Content Cell<ahref="https://www.example.com/"name="namehere">Example</a><pid="paragraph"class="klass">Paragraph</p>{%endmarkdown%}Theextensions=andextra_tags=attributes accept a space-separated list of values.Theextra_attrs=attribute accepts a space-separated list oftag:attr1,attr2values - each tag can specify one or more attributes that should be allowed.You can also use the{{ render_markdown(...) }}function, like this:{{render_markdown("""## Markdown tableFirst Header  | Second Header------------- | -------------Content Cell  | Content CellContent Cell  | Content Cell""",extensions=["tables"],extra_tags=["table","thead","tr","th","td","tbody"]))}}The{% markdown %}tag is recommended, as it avoids the need to\"escape quotes in your Markdown content.