CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
encode-info.js83 linesDownload Raw Back to util
1/**2 * @import {EncodeSides} from '../types.js'3 */4 5import {classifyCharacter} from 'micromark-util-classify-character'6 7/**8 * Check whether to encode (as a character reference) the characters9 * surrounding an attention run.10 *11 * Which characters are around an attention run influence whether it works or12 * not.13 *14 * See <https://github.com/orgs/syntax-tree/discussions/60> for more info.15 * See this markdown in a particular renderer to see what works:16 *17 * ```markdown18 * |                         | A (letter inside) | B (punctuation inside) | C (whitespace inside) | D (nothing inside) |19 * | ----------------------- | ----------------- | ---------------------- | --------------------- | ------------------ |20 * | 1 (letter outside)      | x*y*z             | x*.*z                  | x* *z                 | x**z               |21 * | 2 (punctuation outside) | .*y*.             | .*.*.                  | .* *.                 | .**.               |22 * | 3 (whitespace outside)  | x *y* z           | x *.* z                | x * * z               | x ** z             |23 * | 4 (nothing outside)     | *x*               | *.*                    | * *                   | **                 |24 * ```25 *26 * @param {number} outside27 *   Code point on the outer side of the run.28 * @param {number} inside29 *   Code point on the inner side of the run.30 * @param {'*' | '_'} marker31 *   Marker of the run.32 *   Underscores are handled more strictly (they form less often) than33 *   asterisks.34 * @returns {EncodeSides}35 *   Whether to encode characters.36 */37// Important: punctuation must never be encoded.38// Punctuation is solely used by markdown constructs.39// And by encoding itself.40// Encoding them will break constructs or double encode things.41export function encodeInfo(outside, inside, marker) {42  const outsideKind = classifyCharacter(outside)43  const insideKind = classifyCharacter(inside)44 45  // Letter outside:46  if (outsideKind === undefined) {47    return insideKind === undefined48      ? // Letter inside:49        // we have to encode *both* letters for `_` as it is looser.50        // it already forms for `*` (and GFMs `~`).51        marker === '_'52        ? {inside: true, outside: true}53        : {inside: false, outside: false}54      : insideKind === 155        ? // Whitespace inside: encode both (letter, whitespace).56          {inside: true, outside: true}57        : // Punctuation inside: encode outer (letter)58          {inside: false, outside: true}59  }60 61  // Whitespace outside:62  if (outsideKind === 1) {63    return insideKind === undefined64      ? // Letter inside: already forms.65        {inside: false, outside: false}66      : insideKind === 167        ? // Whitespace inside: encode both (whitespace).68          {inside: true, outside: true}69        : // Punctuation inside: already forms.70          {inside: false, outside: false}71  }72 73  // Punctuation outside:74  return insideKind === undefined75    ? // Letter inside: already forms.76      {inside: false, outside: false}77    : insideKind === 178      ? // Whitespace inside: encode inner (whitespace).79        {inside: true, outside: false}80      : // Punctuation inside: already forms.81        {inside: false, outside: false}82}83