Created
January 31, 2025 11:14
-
-
Save agoose77/4906e4fca15ce87f0147369e9222dc97 to your computer and use it in GitHub Desktop.
A plugin to highlight table cells starting with particular substrings
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
const COLOURS = { | |
"positively charged": "#CBE4F9", | |
"negatively charged": "#CDF5F6", | |
"polar uncharged": "#EFF9DA", | |
hydrophobic: "#F9EBDF", | |
"hydrophobic and aromatic": "#F9D8D6", | |
special: "#D6CDEA", | |
}; | |
const plugin = { | |
name: "Bioinformatics", | |
transforms: [ | |
{ | |
name: "highlight-amino-aids", | |
doc: "A transform that highlights table rows containing certain amino acids", | |
stage: "document", | |
plugin: (_, utils) => (node) => { | |
utils | |
// Find the rows of all tables with label "amino-acids" | |
.selectAll("container[class^=amino-acids] table tableRow", node) | |
.forEach((tableRow) => { | |
const tableCells = utils.selectAll("tableCell", tableRow); | |
const tableCellValues = tableCells.map((tableCell) => | |
// Check if the text representation of the cell starts with our pattern | |
utils | |
.selectAll("text", tableCell) | |
.map((child) => child.value) | |
.join("") | |
.toLowerCase(), | |
); | |
// Loop over possible colours | |
for (const [pattern, colour] of Object.entries(COLOURS)) { | |
if ( | |
tableCellValues.some((value) => | |
// Check if the text representation of the cell starts with our pattern | |
value.startsWith(pattern), | |
) | |
) { | |
tableRow["style"] = { | |
"background-color": colour, | |
}; | |
} | |
} | |
}); | |
}, | |
}, | |
], | |
}; | |
export default plugin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this to your
myst.yaml
underproject.plugins
, e.g.