Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in-line config overrides via weird JSON syntax #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 67 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,79 @@ export default class ChemPlugin extends Plugin {
theme: string,
width: string
) => {
// can put options in calling the drawer
const drawer = new SmilesDrawer.SmiDrawer(options);

// different behavior in live preview and view mode
// under view mode, an extra '\n' is appended
const rows = source.split('\n').filter((row) => row.length > 0);

// can put options in calling the drawer
let drawer = new SmilesDrawer.SmiDrawer(options);

// Weird hack to allow in-line config overrides via this syntax:
/* ```smiles
!{"atomVisualization": "default", "terminalCarbons": true}!
S[Ll][Ba]NCOC
CC12CCC3C(C1CCC2O)CCC4=C3C=CC(=C4)O
!{
"atomVisualization": "default",
"terminalCarbons": false,
"COMMENT42069": "Any unknown key is ignored and can be used as a comment",
"DumbComment": "This is the default config btw, you can also put an empty cfg (e.g '!{}!')...",
"DumberCmmnt": "here to reset all the values instead of manually reseting the ones you changed"
}!
S[Ll][Ba]NCOC
CC12CCC3C(C1CCC2O)CCC4=C3C=CC(=C4)O
!{ "atomVisualization" : "balls"
, "terminalCarbons" : true
, "//0": "As long as '!{' starts a line, and the '}!' ends a line..."
, "//1": "the whitespace in the JSON is ignored and you can format how u wish"
}!
S[Ll][Ba]NCOC
CC12CCC3C(C1CCC2O)CCC4=C3C=CC(=C4)O
``` */
let inCfg = false;
let currCfgStr = "";

for (let i = 0; i < rows.length; i++) {
if (rows[i].startsWith("!")) {
currCfgStr = "";
let e = rows[i].slice(1);
if (e.length > 0) {
if (e.endsWith("!")) {
e = e.slice(0,-1);
inCfg = !inCfg;
}
currCfgStr += e;
}

inCfg = !inCfg;
continue;
} else if (inCfg) {
let e = rows[i];
if (e.endsWith("!")) {
e = e.slice(0,-1);
inCfg = !inCfg;
}
currCfgStr += e;
continue;
}

if (currCfgStr != "") {
currCfgStr.replace("\n", "");
const cfgOverrides = JSON.parse(currCfgStr);
const newOptsJs = JSON.parse(JSON.stringify(options));

for (const k in newOptsJs) {
if (cfgOverrides.hasOwnProperty(k)) {
newOptsJs[k] = cfgOverrides[k];
}
}

drawer = new SmilesDrawer.SmiDrawer(newOptsJs);
}
// End of in-line config override hack lol

const img = container.createEl('img') as HTMLImageElement;

img.width = parseInt(width);
drawer.draw(
rows[i].trim(), // handle spaces in front of the string
Expand Down