npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

persian-katex-plugin

v1.1.0

Published

KaTeX plugin to render formulas with persian characters

Downloads

179

Readme

This is a KaTeX plugin that adds support for Persian math formulas. It adds the following to the KaTeX:

  1. Support for Persian and Arabic numerals [۰-۹] in formulas (math mode)
  2. Support for Persian and Arabic characters [unicode 0x0660 to 0x06FF] in text inside formules (text mode) All the Persian and Arabic characters use open source Vazir font.

To use this plugin follow KaTeX usage guidelines in order to add KaTeX to your environment.

Then install the plugin by:

npm

  • install package: npm install persian-katex-plugin or yarn add persian-katex-plugin
  • import css files using a bundler like webpack and css-loader: import 'perisan-katex-plugin/dist/index.css'
  • you also need file-loader in your webpack config in order for the plugin to load font files.

Finally you need to add the plugin to KaTeX before calling katex.render. Your final code should be something like this:

import katex from 'katex';
import 'katex/dist/katex.css';
import addPersianTo from 'persian-katex-plugin';
import 'perisan-katex-plugin/dist/index.css';

addPersianTo(katex);

All the persian and arabic characters will render using the 'Vazir' font. You should change your western numeral characters ([0-9]) to eastern numerals [۰-۹].

changing the font

As KaTeX calculates the character sizes statically (at compile time) using a new font for your formulas is not as easy as changing the font of a css class.
To change the font of Persian and Arabic characters and numerals you need to first calculate the character metrics for your font and then provide the metrics and fontName to the plugin as its second options paramter.

  1. using persian-katex-plugin/utils/generateMetrics.js script you can create a json file for you metrics. Following is an example for how we generated default Vazir font metrics.
    You have to provide the path to truetype (ttf) files of your font (metrics are usually the same for other file types).
    You also need to provide each font name in the following format: [fontname]-[Regular/Bold/Italic/BoldItalic].
    You can also provide an aditional unicodeRange array to define which range of unicode characters you want to extract from font file.
const generateMetrics = require("perisan-katex-plugin/utils/generateMetrics");
const path = require("path");

const metricOptions = [
    {
        fontname: 'Vazir-Regular',
        filepath: path.resolve("fonts/Vazir.ttf"),
    //  unicodeRange: defualts to [0x0600, 0x06FF]
    },
    {
        fontname: 'Vazir-Bold',
        filepath: path.resolve("fonts/Vazir-Bold.ttf"),
    },
];
const outputPath = path.resolve(__dirname, "src/fontMetrics.json");

generateMetrics(metricOptions, outputPath);
  1. After generating the font metrics file you need to add the metrics and font name options to the plugin.
import katex from 'katex';
import 'katex/dist/katex.css';
import addPersianTo from 'persian-katex-plugin';
import 'perisan-katex-plugin/dist/index.css';
import awesomeFontMetrics from 'path-to-awesomeFontMetrics.js';

addPersianTo(katex, {
  fontName: "MyAwesomeFont",
  fontMetrics: awesomeFontMetrics
});
  1. finally add a font-face and following css class for each fontname you provided to generateFontMetrics.
@font-face {
    font-family: 'AwesomeFont-Regular';
    font-weight: normal;
    font-style: normal;
    src: url('../fonts/AwesomeFont.woff2') format('woff2'),
        url('../fonts/AwesomeFont.woff') format('woff'),
        url('../fonts/AwesomeFont.ttf') format('ttf');
}

@font-face {
    font-family: 'AwesomeFont-Bold';
    font-weight: bold;
    font-style: normal;
    src: url('../fonts/AwesomeFont-Bold.woff2') format('woff2'),
        url('../fonts/AwesomeFont-Bold.woff') format('woff'),
        url('../fonts/AwesomeFont-Bold.ttf') format('ttf');
}

// bidi algorithm generates wrong result for 
// inline persian and english \text combinations 
// so we make all \text inline-block
.katex .mord.text .AwesomeFont-Regular, 
.katex .mord.text .AwesomeFont-Bold {
    display: inline-block;
}

.AwesomeFont-Regular {
    font-family: 'AwesomeFont-Regular';
}

.AwesomeFont-Bold {
    font-family: 'AwesomeFont-Bold';
}

PRs Welcome

run yarn install then yarn run dev to run development server.