Skip to content

Commit 8620871

Browse files
committed
Replace math images with MathJAX.
1 parent b6d21d0 commit 8620871

4 files changed

Lines changed: 42 additions & 20 deletions

File tree

converter/generator/DocToStaticPagesTransformer.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,16 @@ protected internal override void Transform(IHtmlDocument document, IHtmlHeadElem
8080

8181
protected override IHtmlElement? TransformImage(IHtmlDocument document, IHtmlImageElement img, string sourceFile, string sourceDir)
8282
{
83-
if (base.TransformImage(document, img, sourceFile, sourceDir) is IHtmlElement transformed)
83+
if (img.ClassList.Contains("tex"))
84+
{
85+
var span = document.CreateElement<IHtmlSpanElement>();
86+
87+
span.ClassName = "tex";
88+
span.TextContent = $"\\({img.AlternativeText}\\)";
89+
90+
return span;
91+
}
92+
else if (base.TransformImage(document, img, sourceFile, sourceDir) is IHtmlElement transformed)
8493
{
8594
transformed.SetAttribute("loading", "lazy");
8695

converter/generator/DocTransformer.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,18 @@ internal protected virtual void Transform(IHtmlDocument document, IHtmlHeadEleme
9393

9494
foreach (var a in document.Descendants<IHtmlAnchorElement>())
9595
{
96-
TransformAnchor(document, a, sourceFile, sourceDir);
96+
if (TransformAnchor(document, a, sourceFile, sourceDir) is IHtmlElement transformed && transformed != a)
97+
{
98+
a.Replace(transformed);
99+
}
97100
}
98101

99102
foreach (var img in document.Descendants<IHtmlImageElement>())
100103
{
101-
TransformImage(document, img, sourceFile, sourceDir);
104+
if (TransformImage(document, img, sourceFile, sourceDir) is IHtmlElement transformed && transformed != img)
105+
{
106+
img.Replace(transformed);
107+
}
102108
}
103109
}
104110

converter/templates/Layouts/Site.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
1414
<script src="~/global/originlab.js" asp-append-version="true"></script>
1515

16-
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML'></script>
17-
1816
<link href="~/books/style.css" asp-append-version="true" rel="stylesheet" />
1917
<script src="~/books/script.js" asp-append-version="true"></script>
2018

19+
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML'></script>
20+
2121
@RenderSection("head", required: false)
2222
</head>
2323
<body>

converter/templates/wwwroot/books/script.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,6 @@ function searchDoc(lang, inputId, selectId) {
3939
document.location.replace(searchUrl);
4040
}
4141

42-
function applyNavMenuData(dataId, groupId, minItems) {
43-
let dataElement = document.getElementById(dataId);
44-
if (!dataElement || dataElement.children.length < minItems) {
45-
document.getElementById(groupId)?.querySelector('button')?.classList.add('disabled');
46-
} else {
47-
let ul = document.getElementById(groupId)?.querySelector('ul');
48-
if (ul) {
49-
ul.replaceChildren(...dataElement.childNodes);
50-
if (ul.childNodes.length > 20) {
51-
ul.classList.add('pre-scrollable');
52-
}
53-
}
54-
}
55-
}
56-
5742
(function () {
5843
let nav = document.getElementById('doc-nav-data');
5944

@@ -89,6 +74,28 @@ function applyNavMenuData(dataId, groupId, minItems) {
8974
nav.appendChild(data);
9075
}
9176

77+
function applyNavMenuData(dataId, groupId, minItems) {
78+
let dataElement = document.getElementById(dataId);
79+
if (!dataElement || dataElement.children.length < minItems) {
80+
document.getElementById(groupId)?.querySelector('button')?.classList.add('disabled');
81+
} else {
82+
let ul = document.getElementById(groupId)?.querySelector('ul');
83+
if (ul) {
84+
ul.replaceChildren(...dataElement.childNodes);
85+
if (ul.childNodes.length > 20) {
86+
ul.classList.add('pre-scrollable');
87+
}
88+
}
89+
}
90+
}
91+
9292
applyNavMenuData('doc-siblings-data', 'doc-nav-siblings', 2);
9393
applyNavMenuData('doc-children-data', 'doc-nav-children', 1);
9494
})();
95+
96+
window.MathJax = {
97+
options: {
98+
processHtmlClass: 'tex',
99+
ignoreHtmlClass: '.*'
100+
}
101+
};

0 commit comments

Comments
 (0)