-
Notifications
You must be signed in to change notification settings - Fork 129
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
dot, tex, and mermaid blocks #8
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import {Parser, TokContext, tokTypes as tt} from "acorn"; | ||
import {Sourcemap} from "./sourcemap.js"; | ||
|
||
const CODE_DOLLAR = 36; | ||
const CODE_BACKSLASH = 92; | ||
const CODE_BACKTICK = 96; | ||
const CODE_BRACEL = 123; | ||
|
||
export function transpileTag(input, tag = "", raw = false) { | ||
const options = {ecmaVersion: 13, sourceType: "module"}; | ||
const template = TemplateParser.parse(input, options); | ||
const source = new Sourcemap(input); | ||
escapeTemplateElements(source, template, raw); | ||
source.insertLeft(template.start, tag + "`"); | ||
source.insertRight(template.end, "`"); | ||
return String(source); | ||
} | ||
|
||
class TemplateParser extends Parser { | ||
constructor(...args) { | ||
super(...args); | ||
// Initialize the type so that we're inside a backQuote | ||
this.type = tt.backQuote; | ||
this.exprAllowed = false; | ||
} | ||
initialContext() { | ||
// Provide our custom TokContext | ||
return [o_tmpl]; | ||
} | ||
parseTopLevel(body) { | ||
// Fix for nextToken calling finishToken(tt.eof) | ||
if (this.type === tt.eof) this.value = ""; | ||
// Based on acorn.Parser.parseTemplate | ||
const isTagged = true; | ||
body.expressions = []; | ||
let curElt = this.parseTemplateElement({isTagged}); | ||
body.quasis = [curElt]; | ||
while (this.type !== tt.eof) { | ||
this.expect(tt.dollarBraceL); | ||
body.expressions.push(this.parseExpression()); | ||
this.expect(tt.braceR); | ||
body.quasis.push((curElt = this.parseTemplateElement({isTagged}))); | ||
} | ||
curElt.tail = true; | ||
this.next(); | ||
this.finishNode(body, "TemplateLiteral"); | ||
this.expect(tt.eof); | ||
return body; | ||
} | ||
} | ||
|
||
// Based on acorn’s q_tmpl. We will use this to initialize the | ||
// parser context so our `readTemplateToken` override is called. | ||
// `readTemplateToken` is based on acorn's `readTmplToken` which | ||
// is used inside template literals. Our version allows backQuotes. | ||
const o_tmpl = new TokContext( | ||
"`", // token | ||
true, // isExpr | ||
true, // preserveSpace | ||
(parser) => readTemplateToken.call(parser) // override | ||
); | ||
|
||
// This is our custom override for parsing a template that allows backticks. | ||
// Based on acorn's readInvalidTemplateToken. | ||
function readTemplateToken() { | ||
out: for (; this.pos < this.input.length; this.pos++) { | ||
switch (this.input.charCodeAt(this.pos)) { | ||
case CODE_BACKSLASH: { | ||
if (this.pos < this.input.length - 1) ++this.pos; // not a terminal slash | ||
break; | ||
} | ||
case CODE_DOLLAR: { | ||
if (this.input.charCodeAt(this.pos + 1) === CODE_BRACEL) { | ||
if (this.pos === this.start && this.type === tt.invalidTemplate) { | ||
this.pos += 2; | ||
return this.finishToken(tt.dollarBraceL); | ||
} | ||
break out; | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
return this.finishToken(tt.invalidTemplate, this.input.slice(this.start, this.pos)); | ||
} | ||
|
||
function escapeTemplateElements(source, {quasis}, raw) { | ||
for (const quasi of quasis) { | ||
if (raw) { | ||
interpolateBacktick(source, quasi); | ||
} else { | ||
escapeBacktick(source, quasi); | ||
escapeBackslash(source, quasi); | ||
} | ||
} | ||
if (raw) interpolateTerminalBackslash(source); | ||
} | ||
|
||
function escapeBacktick(source, {start, end}) { | ||
const input = source._input; | ||
for (let i = start; i < end; ++i) { | ||
if (input.charCodeAt(i) === CODE_BACKTICK) { | ||
source.insertRight(i, "\\"); | ||
} | ||
} | ||
} | ||
|
||
function interpolateBacktick(source, {start, end}) { | ||
const input = source._input; | ||
let oddBackslashes = false; | ||
for (let i = start; i < end; ++i) { | ||
switch (input.charCodeAt(i)) { | ||
case CODE_BACKSLASH: { | ||
oddBackslashes = !oddBackslashes; | ||
break; | ||
} | ||
case CODE_BACKTICK: { | ||
if (!oddBackslashes) { | ||
let j = i + 1; | ||
while (j < end && input.charCodeAt(j) === CODE_BACKTICK) ++j; | ||
source.replaceRight(i, j, `\${'${"`".repeat(j - i)}'}`); | ||
i = j - 1; | ||
} | ||
// fall through | ||
} | ||
default: { | ||
oddBackslashes = false; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function escapeBackslash(source, {start, end}) { | ||
const input = source._input; | ||
let afterDollar = false; | ||
let oddBackslashes = false; | ||
for (let i = start; i < end; ++i) { | ||
switch (input.charCodeAt(i)) { | ||
case CODE_DOLLAR: { | ||
afterDollar = true; | ||
oddBackslashes = false; | ||
break; | ||
} | ||
case CODE_BACKSLASH: { | ||
oddBackslashes = !oddBackslashes; | ||
if (afterDollar && input.charCodeAt(i + 1) === CODE_BRACEL) continue; | ||
if (oddBackslashes && input.charCodeAt(i + 1) === CODE_DOLLAR && input.charCodeAt(i + 2) === CODE_BRACEL) | ||
continue; | ||
source.insertRight(i, "\\"); | ||
break; | ||
} | ||
default: { | ||
afterDollar = false; | ||
oddBackslashes = false; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function interpolateTerminalBackslash(source) { | ||
const input = source._input; | ||
let oddBackslashes = false; | ||
for (let i = input.length - 1; i >= 0; i--) { | ||
if (input.charCodeAt(i) === CODE_BACKSLASH) oddBackslashes = !oddBackslashes; | ||
else break; | ||
} | ||
if (oddBackslashes) source.replaceRight(input.length - 1, input.length, "${'\\\\'}"); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
```dot | ||
digraph D { | ||
|
||
A [shape=diamond] | ||
B [shape=box] | ||
C [shape=circle] | ||
|
||
A -> B [style=dashed] | ||
A -> C | ||
A -> D [penwidth=5, arrowhead=none] | ||
|
||
} | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
```mermaid | ||
graph TD; | ||
A-->B; | ||
A-->C; | ||
B-->D; | ||
C-->D; | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```tex | ||
\int_0^1 (x + y)dx | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div id="cell-391a3a26" class="observablehq observablehq--block"></div> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"data": null, | ||
"title": null, | ||
"files": [], | ||
"imports": [], | ||
"pieces": [ | ||
{ | ||
"type": "html", | ||
"id": "", | ||
"cellIds": [ | ||
"391a3a26" | ||
], | ||
"html": "<div id=\"cell-391a3a26\" class=\"observablehq observablehq--block\"></div>\n" | ||
} | ||
], | ||
"cells": [ | ||
{ | ||
"type": "cell", | ||
"id": "391a3a26", | ||
"inputs": [ | ||
"dot", | ||
"display" | ||
], | ||
"body": "(dot,display) => {\ndisplay((\ndot`digraph D {\n\n A [shape=diamond]\n B [shape=box]\n C [shape=circle]\n\n A -> B [style=dashed]\n A -> C\n A -> D [penwidth=5, arrowhead=none]\n\n}\n`\n))\n}" | ||
} | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div id="cell-1ffa6d4f" class="observablehq observablehq--block"></div> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"data": null, | ||
"title": null, | ||
"files": [], | ||
"imports": [], | ||
"pieces": [ | ||
{ | ||
"type": "html", | ||
"id": "", | ||
"cellIds": [ | ||
"1ffa6d4f" | ||
], | ||
"html": "<div id=\"cell-1ffa6d4f\" class=\"observablehq observablehq--block\"></div>\n" | ||
} | ||
], | ||
"cells": [ | ||
{ | ||
"type": "cell", | ||
"id": "1ffa6d4f", | ||
"inputs": [ | ||
"mermaid", | ||
"display" | ||
], | ||
"body": "async (mermaid,display) => {\ndisplay((\nawait mermaid`graph TD;\n A-->B;\n A-->C;\n B-->D;\n C-->D;\n`\n))\n}" | ||
} | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div id="cell-5ceee90d" class="observablehq observablehq--block"></div> |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To pass attributes to the tag function, this might need to be something like
new Function(make${tag}(JSON.stringify(attrs) ))
or maybetag + ".bind(" + JSON.stringify(attrs) + ")"