feat: latex in Traverser
This commit is contained in:
parent
00cfde6e90
commit
917fe2f5d2
5 changed files with 72 additions and 2 deletions
|
|
@ -49,7 +49,8 @@ impl Traverser for MyHtmlHandler {
|
|||
inline_src inline_call code bold verbatim italic strike underline list list_item
|
||||
special_block quote_block center_block verse_block comment_block example_block export_block
|
||||
source_block babel_call clock cookie radio_target drawer dyn_block fn_def fn_ref macros
|
||||
snippet timestamp target fixed_width org_table org_table_row org_table_cell
|
||||
snippet timestamp target fixed_width org_table org_table_row org_table_cell latex_fragment
|
||||
latex_environment
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -48,7 +48,8 @@
|
|||
/// inline_src inline_call code bold verbatim italic strike underline list list_item
|
||||
/// special_block quote_block center_block verse_block comment_block example_block export_block
|
||||
/// source_block babel_call clock cookie radio_target drawer dyn_block fn_def fn_ref macros
|
||||
/// snippet timestamp target fixed_width org_table org_table_row org_table_cell
|
||||
/// snippet timestamp target fixed_width org_table org_table_row org_table_cell latex_fragment
|
||||
/// latex_environment
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
|
|
@ -190,6 +191,12 @@ macro_rules! forward_handler {
|
|||
(@method $handler:ty, link) => {
|
||||
forward_handler!(@method $handler, link, WalkEvent<&$crate::ast::Link>);
|
||||
};
|
||||
(@method $handler:ty, latex_fragment) => {
|
||||
forward_handler!(@method $handler, latex_fragment, WalkEvent<&$crate::ast::LatexFragment>);
|
||||
};
|
||||
(@method $handler:ty, latex_environment) => {
|
||||
forward_handler!(@method $handler, latex_environment, WalkEvent<&$crate::ast::LatexEnvironment>);
|
||||
};
|
||||
(@method $handler:ty, $x:ident) => {
|
||||
std::compile_error!(std::concat!(std::stringify!($x), " is not a method"));
|
||||
};
|
||||
|
|
|
|||
|
|
@ -441,4 +441,24 @@ impl Traverser for HtmlExport {
|
|||
|
||||
#[tracing::instrument(skip(self, _ctx))]
|
||||
fn target(&mut self, _event: WalkEvent<&Target>, _ctx: &mut TraversalContext) {}
|
||||
|
||||
#[tracing::instrument(skip(self, ctx))]
|
||||
fn latex_fragment(&mut self, event: WalkEvent<&LatexFragment>, ctx: &mut TraversalContext) {
|
||||
if let WalkEvent::Enter(l) = event {
|
||||
self.output += &l.syntax.to_string();
|
||||
ctx.skip();
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, ctx))]
|
||||
fn latex_environment(
|
||||
&mut self,
|
||||
event: WalkEvent<&LatexEnvironment>,
|
||||
ctx: &mut TraversalContext,
|
||||
) {
|
||||
if let WalkEvent::Enter(l) = event {
|
||||
self.output += &l.syntax.to_string();
|
||||
ctx.skip();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -137,6 +137,8 @@ pub trait Traverser {
|
|||
ORG_TABLE_RULE_ROW | ORG_TABLE_STANDARD_ROW => traverse!(OrgTableRow, org_table_row),
|
||||
ORG_TABLE_CELL => traverse!(OrgTableCell, org_table_cell),
|
||||
LINK => traverse!(Link, link),
|
||||
LATEX_FRAGMENT => traverse!(LatexFragment, latex_fragment),
|
||||
LATEX_ENVIRONMENT => traverse!(LatexEnvironment, latex_environment),
|
||||
|
||||
BLOCK_CONTENT | LIST_ITEM_CONTENT => traverse_children!(node),
|
||||
|
||||
|
|
@ -236,4 +238,12 @@ pub trait Traverser {
|
|||
fn org_table_cell(&mut self, _event: WalkEvent<&OrgTableCell>, _ctx: &mut TraversalContext);
|
||||
/// Called when entering or leaving `Link` node
|
||||
fn link(&mut self, _event: WalkEvent<&Link>, _ctx: &mut TraversalContext);
|
||||
/// Called when entering or leaving `LatexFragment` node
|
||||
fn latex_fragment(&mut self, _event: WalkEvent<&LatexFragment>, _ctx: &mut TraversalContext);
|
||||
/// Called when entering or leaving `LatexEnvironment` node
|
||||
fn latex_environment(
|
||||
&mut self,
|
||||
_event: WalkEvent<&LatexEnvironment>,
|
||||
_ctx: &mut TraversalContext,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,11 @@
|
|||
href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.2.0/github-markdown-light.min.css"
|
||||
/>
|
||||
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css"
|
||||
/>
|
||||
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.22.0/ace.min.js"></script>
|
||||
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.0/beautify-html.js"></script>
|
||||
|
|
@ -20,6 +25,7 @@
|
|||
html {
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.bordered {
|
||||
|
|
@ -109,6 +115,7 @@
|
|||
|
||||
<script type="module">
|
||||
import init, { Org } from "./dist/orgize.js";
|
||||
import renderMathInElement from "https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.mjs";
|
||||
|
||||
let org;
|
||||
const result = document.getElementById("result");
|
||||
|
|
@ -131,6 +138,15 @@
|
|||
const html = injectHeadingClass(org.html());
|
||||
result.innerHTML =
|
||||
"<div style='padding:1.25rem 1.5rem'>" + html + "</div>";
|
||||
renderMathInElement(result, {
|
||||
delimiters: [
|
||||
{ left: "$$", right: "$$", display: true },
|
||||
{ left: "$", right: "$", display: false },
|
||||
{ left: "\\(", right: "\\)", display: false },
|
||||
{ left: "\\[", right: "\\]", display: true },
|
||||
{ left: "\\begin{align}", right: "\\end{align}", display: true },
|
||||
],
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -276,6 +292,22 @@ Table
|
|||
Image
|
||||
|
||||
[[https://www.rust-lang.org/static/images/rust-logo-blk.svg]]
|
||||
|
||||
-----
|
||||
LaTeX
|
||||
|
||||
Render with \\(\\KaTeX\\): $x+y$
|
||||
|
||||
$$
|
||||
f(\\relax{x}) = \\int_{-\\infty}^\\infty
|
||||
\\hat{f}(\\xi)\\,e^{2 \\pi i \\xi x}
|
||||
\\,d\\xi
|
||||
$$
|
||||
|
||||
\\begin{align}
|
||||
a&=b+c \\\\
|
||||
d+e&=f
|
||||
\\end{align}
|
||||
`);
|
||||
|
||||
editor.session.on("change", () => render());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue