I'm writing a brief Quarto document where I explain some SQL clauses/keywords. I'd like to have all of those clauses to be highlighted in my code chunks, but not all of them are supported by default syntax highlighting. Is there a way to add custom keywords to syntax highlighting settings?
For example, I wrote the following code chunks:
SELECT col_name(s)
FROM tbl_name
LIMIT n OFFSET m
-- Syntax supported by MySQL, MariaDB, PostgreSQL, SQLite
-- LIMIT = number of rows to retrieve
-- OFFSET = starting row (optional)
SELECT TOP n col_name(s)
FROM tbl_name
-- Syntax supported by SQL Server and MS Access
And i got the following outputs in my rendered document:
I want both OFFSET
and TOP
to be highlighted in orange, like SELECT
, FROM
etc. How can I do that?
There are several SQL highlight variants, you can get a list of those with quarto pandoc --list-highlight-languages
and switching from sql
to sqlpostgresql
will get you highlighted OFFSET
.
It's surprisingly easy to customize any of the existing highlights, https://pandoc.org/MANUAL.html#syntax-highlighting points you to KDE-style XML syntax definition files, https://github.com/KDE/syntax-highlighting/tree/master/data/syntax .
Pick sql.xml
(or any other ) as a base and add additional keywords:
...
<list name="keywords">
<item>OFFSET</item>
<item>TOP</item>
...
I also changed the name attribute (<language name="SQL_CUSTOM" ...
and saved it next to Quarto qmd as sql_custom.xml
, having all files in a working directory, Quarto / Pandoc picked up updated definition file once it was added to syntax-definitions
list.
Sample qmd:
---
title: "custom syntax hl"
format: html
theme: solar
syntax-definitions:
- sql_custom.xml
editor: source
---
### sql
``` sql
SELECT col_name(s) FROM tbl_name LIMIT n OFFSET m
SELECT TOP n col_name(s) FROM tbl_name
```
### sqlpostgresql knows about `OFFSET`
``` sqlpostgresql
SELECT col_name(s) FROM tbl_name LIMIT n OFFSET m
SELECT TOP n col_name(s) FROM tbl_name
```
### sql_custom knows about `OFFSET` & `TOP`
``` sql_custom
SELECT col_name(s) FROM tbl_name LIMIT n OFFSET m
SELECT TOP n col_name(s) FROM tbl_name
```