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

Can you adapt to the latest version of KAtex? #24

Closed
Tracked by #28
yfblock opened this issue Jul 3, 2022 · 2 comments
Closed
Tracked by #28

Can you adapt to the latest version of KAtex? #24

yfblock opened this issue Jul 3, 2022 · 2 comments

Comments

@yfblock
Copy link

yfblock commented Jul 3, 2022

Please upgrade katex version to 0.16.0, which supports more functions.

@dzylikecode
Copy link
Contributor

You can replace the original position about Doctify-Katex as below.

<head>
  <!-- begin for docsify katex -->
  <link
    rel="stylesheet"
    href="https://cdn.jsdelivr.net/npm/katex@0.16.2/dist/katex.min.css"
    integrity="sha384-bYdxxUwYipFNohQlHt0bjN/LCpueqWz13HufFEV1SUatKs1cm4L6fFgCi1jT643X"
    crossorigin="anonymous"
  />
  <script
    defer
    src="https://cdn.jsdelivr.net/npm/katex@0.16.2/dist/katex.min.js"
    integrity="sha384-Qsn9KnoKISj6dI8g7p1HBlNpVx0I8p1SvlwOldgi3IorMle61nQy4zEahWYtljaz"
    crossorigin="anonymous"
  ></script>
  <!-- the version of marked must be less than 4 -->
  <script src="https://cdn.jsdelivr.net/npm/marked@3"></script>
  <!-- end for docsify katex -->
</head>
<body>
  <div id="app"></div>
  <script src="//cdn.jsdelivr.net/npm/mermaid@8.0.0-rc.8/dist/mermaid.min.js"></script>
  <script>
    window.$docsify = {};
  </script>
  <!-- insert script before docsify.js-->
  <script>
    // import katex from "katex";
    // import marked from "marked";

    (function () {
      let oldMarkdown = window.$docsify.markdown;
      let newMarked = marked;
      let newM = JSON.stringify(newMarked);
      function newMarkdown(originMarked, originRenderer) {
        // let newMarked = marked; // in docsify.js: window.$marked = marked
        function isFn(obj) {
          return typeof obj === "function";
        }
        const mathExtension = {
          name: "math",
          level: "inline",
          start(src) {
            let index = src.match(/\$/)?.index;
            return index;
          },
          tokenizer(src, tokens) {
            const blockRule = /^\$\$((\\.|[^\$\\])+)\$\$/;
            const inlineRule = /^\$((\\.|[^\$\\])+)\$/;
            let match;
            if ((match = blockRule.exec(src))) {
              return {
                type: "math",
                raw: match[0],
                text: match[1].trim(),
                mathLevel: "block",
              };
            } else if ((match = inlineRule.exec(src))) {
              return {
                type: "math",
                raw: match[0],
                text: match[1].trim(),
                mathLevel: "inline",
              };
            }
          },
          renderer(token) {
            if (token.mathLevel === "block") {
              return (
                '<div class="math">' +
                katex.renderToString(token.text, {
                  throwOnError: false,
                  displayMode: true,
                }) +
                "</div>"
              );
            } else if (token.mathLevel === "inline") {
              return (
                '<span class="math">' +
                katex.renderToString(token.text, {
                  throwOnError: false,
                  displayMode: false,
                }) +
                "</span>"
              );
            }
          },
        };
        const merge =
          Object.assign ||
          function (to) {
            for (let i = 1; i < arguments.length; i++) {
              const from = Object(arguments[i]);
              for (const key in from) {
                if (hasOwn.call(from, key)) {
                  to[key] = from[key];
                }
              }
            }
            return to;
          };

        let opts = oldMarkdown || {};

        if (isFn(oldMarkdown)) {
          opts = originMarkdonw.apply(
            this, // this original
            originMarked,
            originRenderer
          ).defaults;
        } else {
          opts = merge(oldMarkdown, {
            renderer: merge(originRenderer, oldMarkdown.renderer),
          });
        }
        newMarked.setOptions(opts);
        newMarked.use({ extensions: [mathExtension] });

        return newMarked;
      }
      window.$docsify.markdown = newMarkdown;
    })();
  </script>
  <script src="//cdn.jsdelivr.net/npm/docsify@4/lib/docsify.js"></script>
</body>

@upupming
Copy link
Owner

upupming commented Sep 7, 2022

Thanks for @dzylikecode 's great work, you can follow the README.md to use whichever katex version you want.

@upupming upupming closed this as completed Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants