在圆圈中配置Katex(代码注入)。

发布于 2025-02-01 12:05:56 字数 1688 浏览 2 评论 0 原文

我正在为IB数学学生建立一个在线社区,为此我计划使用 circle.so 。鉴于其功能(例如代码注入)和设计,Circle.So似乎是一个不错的选择。

论坛工具(例如圆形)需要具有的功能之一是呈现数学符号(乳胶)或允许代码注入的能力,因此可以使用Katex或MathJax。 katex 似乎可以解决问题。我正在测试/尝试安装KATEX,并且可以选择在头部和JavaScript片段中注入代码(如下图所示)。

我的编程技能为零,我想知道是否有人做过或可以帮助我实现这一目标。对于头部部分,我放置了以下代码(它不起作用)。

<!-- The loading of KaTeX is deferred to speed up page rendering -->
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" integrity="sha384-ljao5I1l+8KYFXG7LNEA7DyaFvuvSCmedUf6Y6JI7LJqiu8q5dEivP2nDdFH31V4" crossorigin="anonymous"></script>

<!-- To automatically render math in text elements, include the auto-render extension: -->
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js" integrity="sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR" crossorigin="anonymous"
    onload="renderMathInElement(document.body);"></script>

可以找到Katex的文档在这里

obs:circle.O可以选择插入自定义CSS(如果有帮助)

I am building an online community for IB Math students and for that I plan to use Circle.so. Circle.so seems like a great option given its features (like code injection) and design.

One of the features that the forum tool (such as Circle) needs to have is the ability to render Math notation (LaTeX) or allow code injection so KaTeX or MathJax can be used. KaTeX seems to solve the problem. I am testing/trying to install KaTeX and I have the options to inject code in the head and as a javascript snippet (as shown in the image below).

enter image description here

I have zero programming skills, I wonder if anyone has done it or could help me accomplish that. For the head portion, I placed the following code (it didn't work).

<!-- The loading of KaTeX is deferred to speed up page rendering -->
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" integrity="sha384-ljao5I1l+8KYFXG7LNEA7DyaFvuvSCmedUf6Y6JI7LJqiu8q5dEivP2nDdFH31V4" crossorigin="anonymous"></script>

<!-- To automatically render math in text elements, include the auto-render extension: -->
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js" integrity="sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR" crossorigin="anonymous"
    onload="renderMathInElement(document.body);"></script>

KaTeX's documentation can be found here.

Obs: Circle.so has the option to insert custom CSS (if that helps)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2025-02-08 12:05:56

这是一个2部分问题,一个非常容易。

问题1

解析Katex

这相对容易,

我们在页面上添加脚本和样式,并在我们的自定义脚本上选择和渲染文本。

这可以在标题代码段

下进行

注意:否 defer 属性在JS代码运行之前将其加载。

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.css" integrity="sha384-jJFDzw6Rh7jzIumRp31oSKurBXlvFPBHbzi9KbVukp6ZNECGD4UKyhO5tJaJ1uHA" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.js" integrity="sha384-Xfbu6aqAjKdi+UhRmQROxVxvx/iT6irqZqIPGKTbtyjUVRkdg3aEiaxuMdfVqNyu" crossorigin="anonymous"></script>

还有一些JS渲染Katex字符串。

katex.renderToString( katexText, {
  throwOnError: false
});

未解决的问题

我们不知道的

  1. ...哪个文本呈现Katex。
  2. 在哪里放置渲染文字。

问题2

用户如何插入katex

这是一个充满挑战的一个,因为我们无法控制UI。但是,解决方法可能是使用工具栏中使用代码选项。

​Katex 和关键字只能是 katex 。因此,要使用KATEX,用户将在第一行中添加一个用 katex 添加一个代码块,我们的JS将从那里添加。

这可以在标题代码段

下进行

<script id=''>
    // Find all `pre` tags
    document.querySelectorAll('.trix-content pre').forEach(function(el) {
        // Get text inside
        var text = el.innerText.replace(/^\s+|\s+$/gm, '');
        // Do stuff if text starts with `latex`
        if (0 === text.indexOf('katex')) {
            var renderedKatex = katex.renderToString(
                text.replace('katex', ''),
                {throwOnError: false}
            );
            // Populate katex and additional markup for formatting
            el.outerHTML =
                '<span></span><section class="katex-wrap">' +
                renderedKatex + '</section>';
        }
    })
<script>
之前

”脚本'

scripts

工作片段

// Find all `pre` tags
document.querySelectorAll('.trix-content pre').forEach(function(el) {
  // Get text inside
  var text = el.innerText.replace(/^\s+|\s+$/gm, '');
  // Do stuff if text starts with `latex`
  if (0 === text.indexOf('katex')) {
    var renderedKatex = katex.renderToString(text.replace('katex', ''), {
      throwOnError: false
    });
    // Populat katex and additional markup for formatting
    el.outerHTML = '<span></span><section class="katex-wrap">' + renderedKatex + '</section>';
  }
})
.post__more {
  display: none;
}
<!-- THIS GOES IN HEAD CODE SNIPPETS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.css" integrity="sha384-jJFDzw6Rh7jzIumRp31oSKurBXlvFPBHbzi9KbVukp6ZNECGD4UKyhO5tJaJ1uHA" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.js" integrity="sha384-Xfbu6aqAjKdi+UhRmQROxVxvx/iT6irqZqIPGKTbtyjUVRkdg3aEiaxuMdfVqNyu" crossorigin="anonymous"></script>

<h1>Typical circle.so markup</h1>

<h2>Main post content markup</h2>

<div class="post__body">
  <div class="post__inside trix-v2  expanded">
    <div class="react-trix-content fullscreen-preview-enabled">
      <div>
        <div class="trix-content">
          <pre>katex
c = \pm\sqrt{a^2 + b^2}</pre>
        </div>
      </div>
    </div>
  </div>
</div>

<h2>Comment post content markup</h2>

<div class="post__body" data-controller="post">
  <div class="post__inside">
    <div class="trix-content">
      <div>Ordinary code block,</div>
      <pre>const jsVar = 'mi secreto';</pre>
      <div>
        <br>Latex code block<br><br>
      </div>
      <pre>katex
e = mc^2</pre>
      <div></div>
    </div>
  </div>
</div>

This is a 2 part problem, one pretty easy the other a bit harder.

Problem 1

Parsing KATEX

This is relatively easy,

We add the scripts and styles on the page and our custom script to select and render text.

This can go under Header code snippets

Notice: No defer attribute to make it load before JS code runs.

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.css" integrity="sha384-jJFDzw6Rh7jzIumRp31oSKurBXlvFPBHbzi9KbVukp6ZNECGD4UKyhO5tJaJ1uHA" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.js" integrity="sha384-Xfbu6aqAjKdi+UhRmQROxVxvx/iT6irqZqIPGKTbtyjUVRkdg3aEiaxuMdfVqNyu" crossorigin="anonymous"></script>

And some JS to render KATEX strings.

katex.renderToString( katexText, {
  throwOnError: false
});

Unresolved issues

We don't know...

  1. which text to render Katex.
  2. where to place the rendered text.

Problem 2

How do users insert Katex

This is a challenging one coz we don't control the UI. However a workaround could be to use Code option from the toolbar.

enter image description here

enter image description here

However we don't want to parse and potentially break all code blocks, but only specific ones, for this we could add in a key word to trigger katex and keyword can just be katex. So to use katex, users would add a code block with katex on first line and our JS will take it from there.

This can go under Header code snippets

<script id=''>
    // Find all `pre` tags
    document.querySelectorAll('.trix-content pre').forEach(function(el) {
        // Get text inside
        var text = el.innerText.replace(/^\s+|\s+$/gm, '');
        // Do stuff if text starts with `latex`
        if (0 === text.indexOf('katex')) {
            var renderedKatex = katex.renderToString(
                text.replace('katex', ''),
                {throwOnError: false}
            );
            // Populate katex and additional markup for formatting
            el.outerHTML =
                '<span></span><section class="katex-wrap">' +
                renderedKatex + '</section>';
        }
    })
<script>
Before scripts

Before scripts

After scripts

After scripts

Working snippet

// Find all `pre` tags
document.querySelectorAll('.trix-content pre').forEach(function(el) {
  // Get text inside
  var text = el.innerText.replace(/^\s+|\s+$/gm, '');
  // Do stuff if text starts with `latex`
  if (0 === text.indexOf('katex')) {
    var renderedKatex = katex.renderToString(text.replace('katex', ''), {
      throwOnError: false
    });
    // Populat katex and additional markup for formatting
    el.outerHTML = '<span></span><section class="katex-wrap">' + renderedKatex + '</section>';
  }
})
.post__more {
  display: none;
}
<!-- THIS GOES IN HEAD CODE SNIPPETS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.css" integrity="sha384-jJFDzw6Rh7jzIumRp31oSKurBXlvFPBHbzi9KbVukp6ZNECGD4UKyhO5tJaJ1uHA" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.js" integrity="sha384-Xfbu6aqAjKdi+UhRmQROxVxvx/iT6irqZqIPGKTbtyjUVRkdg3aEiaxuMdfVqNyu" crossorigin="anonymous"></script>

<h1>Typical circle.so markup</h1>

<h2>Main post content markup</h2>

<div class="post__body">
  <div class="post__inside trix-v2  expanded">
    <div class="react-trix-content fullscreen-preview-enabled">
      <div>
        <div class="trix-content">
          <pre>katex
c = \pm\sqrt{a^2 + b^2}</pre>
        </div>
      </div>
    </div>
  </div>
</div>

<h2>Comment post content markup</h2>

<div class="post__body" data-controller="post">
  <div class="post__inside">
    <div class="trix-content">
      <div>Ordinary code block,</div>
      <pre>const jsVar = 'mi secreto';</pre>
      <div>
        <br>Latex code block<br><br>
      </div>
      <pre>katex
e = mc^2</pre>
      <div></div>
    </div>
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文