index.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. import { NodeProp } from '@lezer/common';
  2. let nextTagID = 0;
  3. /// Highlighting tags are markers that denote a highlighting category.
  4. /// They are [associated](#highlight.styleTags) with parts of a syntax
  5. /// tree by a language mode, and then mapped to an actual CSS style by
  6. /// a [highlighter](#highlight.Highlighter).
  7. ///
  8. /// Because syntax tree node types and highlight styles have to be
  9. /// able to talk the same language, CodeMirror uses a mostly _closed_
  10. /// [vocabulary](#highlight.tags) of syntax tags (as opposed to
  11. /// traditional open string-based systems, which make it hard for
  12. /// highlighting themes to cover all the tokens produced by the
  13. /// various languages).
  14. ///
  15. /// It _is_ possible to [define](#highlight.Tag^define) your own
  16. /// highlighting tags for system-internal use (where you control both
  17. /// the language package and the highlighter), but such tags will not
  18. /// be picked up by regular highlighters (though you can derive them
  19. /// from standard tags to allow highlighters to fall back to those).
  20. class Tag {
  21. /// @internal
  22. constructor(
  23. /// The set of this tag and all its parent tags, starting with
  24. /// this one itself and sorted in order of decreasing specificity.
  25. set,
  26. /// The base unmodified tag that this one is based on, if it's
  27. /// modified @internal
  28. base,
  29. /// The modifiers applied to this.base @internal
  30. modified) {
  31. this.set = set;
  32. this.base = base;
  33. this.modified = modified;
  34. /// @internal
  35. this.id = nextTagID++;
  36. }
  37. /// Define a new tag. If `parent` is given, the tag is treated as a
  38. /// sub-tag of that parent, and
  39. /// [highlighters](#highlight.tagHighlighter) that don't mention
  40. /// this tag will try to fall back to the parent tag (or grandparent
  41. /// tag, etc).
  42. static define(parent) {
  43. if (parent === null || parent === void 0 ? void 0 : parent.base)
  44. throw new Error("Can not derive from a modified tag");
  45. let tag = new Tag([], null, []);
  46. tag.set.push(tag);
  47. if (parent)
  48. for (let t of parent.set)
  49. tag.set.push(t);
  50. return tag;
  51. }
  52. /// Define a tag _modifier_, which is a function that, given a tag,
  53. /// will return a tag that is a subtag of the original. Applying the
  54. /// same modifier to a twice tag will return the same value (`m1(t1)
  55. /// == m1(t1)`) and applying multiple modifiers will, regardless or
  56. /// order, produce the same tag (`m1(m2(t1)) == m2(m1(t1))`).
  57. ///
  58. /// When multiple modifiers are applied to a given base tag, each
  59. /// smaller set of modifiers is registered as a parent, so that for
  60. /// example `m1(m2(m3(t1)))` is a subtype of `m1(m2(t1))`,
  61. /// `m1(m3(t1)`, and so on.
  62. static defineModifier() {
  63. let mod = new Modifier;
  64. return (tag) => {
  65. if (tag.modified.indexOf(mod) > -1)
  66. return tag;
  67. return Modifier.get(tag.base || tag, tag.modified.concat(mod).sort((a, b) => a.id - b.id));
  68. };
  69. }
  70. }
  71. let nextModifierID = 0;
  72. class Modifier {
  73. constructor() {
  74. this.instances = [];
  75. this.id = nextModifierID++;
  76. }
  77. static get(base, mods) {
  78. if (!mods.length)
  79. return base;
  80. let exists = mods[0].instances.find(t => t.base == base && sameArray(mods, t.modified));
  81. if (exists)
  82. return exists;
  83. let set = [], tag = new Tag(set, base, mods);
  84. for (let m of mods)
  85. m.instances.push(tag);
  86. let configs = permute(mods);
  87. for (let parent of base.set)
  88. for (let config of configs)
  89. set.push(Modifier.get(parent, config));
  90. return tag;
  91. }
  92. }
  93. function sameArray(a, b) {
  94. return a.length == b.length && a.every((x, i) => x == b[i]);
  95. }
  96. function permute(array) {
  97. let result = [array];
  98. for (let i = 0; i < array.length; i++) {
  99. for (let a of permute(array.slice(0, i).concat(array.slice(i + 1))))
  100. result.push(a);
  101. }
  102. return result;
  103. }
  104. /// This function is used to add a set of tags to a language syntax
  105. /// via [`NodeSet.extend`](#common.NodeSet.extend) or
  106. /// [`LRParser.configure`](#lr.LRParser.configure).
  107. ///
  108. /// The argument object maps node selectors to [highlighting
  109. /// tags](#highlight.Tag) or arrays of tags.
  110. ///
  111. /// Node selectors may hold one or more (space-separated) node paths.
  112. /// Such a path can be a [node name](#common.NodeType.name), or
  113. /// multiple node names (or `*` wildcards) separated by slash
  114. /// characters, as in `"Block/Declaration/VariableName"`. Such a path
  115. /// matches the final node but only if its direct parent nodes are the
  116. /// other nodes mentioned. A `*` in such a path matches any parent,
  117. /// but only a single level—wildcards that match multiple parents
  118. /// aren't supported, both for efficiency reasons and because Lezer
  119. /// trees make it rather hard to reason about what they would match.)
  120. ///
  121. /// A path can be ended with `/...` to indicate that the tag assigned
  122. /// to the node should also apply to all child nodes, even if they
  123. /// match their own style (by default, only the innermost style is
  124. /// used).
  125. ///
  126. /// When a path ends in `!`, as in `Attribute!`, no further matching
  127. /// happens for the node's child nodes, and the entire node gets the
  128. /// given style.
  129. ///
  130. /// In this notation, node names that contain `/`, `!`, `*`, or `...`
  131. /// must be quoted as JSON strings.
  132. ///
  133. /// For example:
  134. ///
  135. /// ```javascript
  136. /// parser.withProps(
  137. /// styleTags({
  138. /// // Style Number and BigNumber nodes
  139. /// "Number BigNumber": tags.number,
  140. /// // Style Escape nodes whose parent is String
  141. /// "String/Escape": tags.escape,
  142. /// // Style anything inside Attributes nodes
  143. /// "Attributes!": tags.meta,
  144. /// // Add a style to all content inside Italic nodes
  145. /// "Italic/...": tags.emphasis,
  146. /// // Style InvalidString nodes as both `string` and `invalid`
  147. /// "InvalidString": [tags.string, tags.invalid],
  148. /// // Style the node named "/" as punctuation
  149. /// '"/"': tags.punctuation
  150. /// })
  151. /// )
  152. /// ```
  153. function styleTags(spec) {
  154. let byName = Object.create(null);
  155. for (let prop in spec) {
  156. let tags = spec[prop];
  157. if (!Array.isArray(tags))
  158. tags = [tags];
  159. for (let part of prop.split(" "))
  160. if (part) {
  161. let pieces = [], mode = 2 /* Normal */, rest = part;
  162. for (let pos = 0;;) {
  163. if (rest == "..." && pos > 0 && pos + 3 == part.length) {
  164. mode = 1 /* Inherit */;
  165. break;
  166. }
  167. let m = /^"(?:[^"\\]|\\.)*?"|[^\/!]+/.exec(rest);
  168. if (!m)
  169. throw new RangeError("Invalid path: " + part);
  170. pieces.push(m[0] == "*" ? "" : m[0][0] == '"' ? JSON.parse(m[0]) : m[0]);
  171. pos += m[0].length;
  172. if (pos == part.length)
  173. break;
  174. let next = part[pos++];
  175. if (pos == part.length && next == "!") {
  176. mode = 0 /* Opaque */;
  177. break;
  178. }
  179. if (next != "/")
  180. throw new RangeError("Invalid path: " + part);
  181. rest = part.slice(pos);
  182. }
  183. let last = pieces.length - 1, inner = pieces[last];
  184. if (!inner)
  185. throw new RangeError("Invalid path: " + part);
  186. let rule = new Rule(tags, mode, last > 0 ? pieces.slice(0, last) : null);
  187. byName[inner] = rule.sort(byName[inner]);
  188. }
  189. }
  190. return ruleNodeProp.add(byName);
  191. }
  192. const ruleNodeProp = new NodeProp();
  193. class Rule {
  194. constructor(tags, mode, context, next) {
  195. this.tags = tags;
  196. this.mode = mode;
  197. this.context = context;
  198. this.next = next;
  199. }
  200. sort(other) {
  201. if (!other || other.depth < this.depth) {
  202. this.next = other;
  203. return this;
  204. }
  205. other.next = this.sort(other.next);
  206. return other;
  207. }
  208. get depth() { return this.context ? this.context.length : 0; }
  209. }
  210. /// Define a [highlighter](#highlight.Highlighter) from an array of
  211. /// tag/class pairs. Classes associated with more specific tags will
  212. /// take precedence.
  213. function tagHighlighter(tags, options) {
  214. let map = Object.create(null);
  215. for (let style of tags) {
  216. if (!Array.isArray(style.tag))
  217. map[style.tag.id] = style.class;
  218. else
  219. for (let tag of style.tag)
  220. map[tag.id] = style.class;
  221. }
  222. let { scope, all = null } = options || {};
  223. return {
  224. style: (tags) => {
  225. let cls = all;
  226. for (let tag of tags) {
  227. for (let sub of tag.set) {
  228. let tagClass = map[sub.id];
  229. if (tagClass) {
  230. cls = cls ? cls + " " + tagClass : tagClass;
  231. break;
  232. }
  233. }
  234. }
  235. return cls;
  236. },
  237. scope: scope
  238. };
  239. }
  240. function highlightTags(highlighters, tags) {
  241. let result = null;
  242. for (let highlighter of highlighters) {
  243. let value = highlighter.style(tags);
  244. if (value)
  245. result = result ? result + " " + value : value;
  246. }
  247. return result;
  248. }
  249. /// Highlight the given [tree](#common.Tree) with the given
  250. /// [highlighter](#highlight.Highlighter).
  251. function highlightTree(tree, highlighter,
  252. /// Assign styling to a region of the text. Will be called, in order
  253. /// of position, for any ranges where more than zero classes apply.
  254. /// `classes` is a space separated string of CSS classes.
  255. putStyle,
  256. /// The start of the range to highlight.
  257. from = 0,
  258. /// The end of the range.
  259. to = tree.length) {
  260. let builder = new HighlightBuilder(from, Array.isArray(highlighter) ? highlighter : [highlighter], putStyle);
  261. builder.highlightRange(tree.cursor(), from, to, "", builder.highlighters);
  262. builder.flush(to);
  263. }
  264. class HighlightBuilder {
  265. constructor(at, highlighters, span) {
  266. this.at = at;
  267. this.highlighters = highlighters;
  268. this.span = span;
  269. this.class = "";
  270. }
  271. startSpan(at, cls) {
  272. if (cls != this.class) {
  273. this.flush(at);
  274. if (at > this.at)
  275. this.at = at;
  276. this.class = cls;
  277. }
  278. }
  279. flush(to) {
  280. if (to > this.at && this.class)
  281. this.span(this.at, to, this.class);
  282. }
  283. highlightRange(cursor, from, to, inheritedClass, highlighters) {
  284. let { type, from: start, to: end } = cursor;
  285. if (start >= to || end <= from)
  286. return;
  287. if (type.isTop)
  288. highlighters = this.highlighters.filter(h => !h.scope || h.scope(type));
  289. let cls = inheritedClass;
  290. let rule = type.prop(ruleNodeProp), opaque = false;
  291. while (rule) {
  292. if (!rule.context || cursor.matchContext(rule.context)) {
  293. let tagCls = highlightTags(highlighters, rule.tags);
  294. if (tagCls) {
  295. if (cls)
  296. cls += " ";
  297. cls += tagCls;
  298. if (rule.mode == 1 /* Inherit */)
  299. inheritedClass += (inheritedClass ? " " : "") + tagCls;
  300. else if (rule.mode == 0 /* Opaque */)
  301. opaque = true;
  302. }
  303. break;
  304. }
  305. rule = rule.next;
  306. }
  307. this.startSpan(cursor.from, cls);
  308. if (opaque)
  309. return;
  310. let mounted = cursor.tree && cursor.tree.prop(NodeProp.mounted);
  311. if (mounted && mounted.overlay) {
  312. let inner = cursor.node.enter(mounted.overlay[0].from + start, 1);
  313. let innerHighlighters = this.highlighters.filter(h => !h.scope || h.scope(mounted.tree.type));
  314. let hasChild = cursor.firstChild();
  315. for (let i = 0, pos = start;; i++) {
  316. let next = i < mounted.overlay.length ? mounted.overlay[i] : null;
  317. let nextPos = next ? next.from + start : end;
  318. let rangeFrom = Math.max(from, pos), rangeTo = Math.min(to, nextPos);
  319. if (rangeFrom < rangeTo && hasChild) {
  320. while (cursor.from < rangeTo) {
  321. this.highlightRange(cursor, rangeFrom, rangeTo, inheritedClass, highlighters);
  322. this.startSpan(Math.min(to, cursor.to), cls);
  323. if (cursor.to >= nextPos || !cursor.nextSibling())
  324. break;
  325. }
  326. }
  327. if (!next || nextPos > to)
  328. break;
  329. pos = next.to + start;
  330. if (pos > from) {
  331. this.highlightRange(inner.cursor(), Math.max(from, next.from + start), Math.min(to, pos), inheritedClass, innerHighlighters);
  332. this.startSpan(pos, cls);
  333. }
  334. }
  335. if (hasChild)
  336. cursor.parent();
  337. }
  338. else if (cursor.firstChild()) {
  339. do {
  340. if (cursor.to <= from)
  341. continue;
  342. if (cursor.from >= to)
  343. break;
  344. this.highlightRange(cursor, from, to, inheritedClass, highlighters);
  345. this.startSpan(Math.min(to, cursor.to), cls);
  346. } while (cursor.nextSibling());
  347. cursor.parent();
  348. }
  349. }
  350. }
  351. const t = Tag.define;
  352. const comment = t(), name = t(), typeName = t(name), propertyName = t(name), literal = t(), string = t(literal), number = t(literal), content = t(), heading = t(content), keyword = t(), operator = t(), punctuation = t(), bracket = t(punctuation), meta = t();
  353. /// The default set of highlighting [tags](#highlight.Tag).
  354. ///
  355. /// This collection is heavily biased towards programming languages,
  356. /// and necessarily incomplete. A full ontology of syntactic
  357. /// constructs would fill a stack of books, and be impractical to
  358. /// write themes for. So try to make do with this set. If all else
  359. /// fails, [open an
  360. /// issue](https://github.com/codemirror/codemirror.next) to propose a
  361. /// new tag, or [define](#highlight.Tag^define) a local custom tag for
  362. /// your use case.
  363. ///
  364. /// Note that it is not obligatory to always attach the most specific
  365. /// tag possible to an element—if your grammar can't easily
  366. /// distinguish a certain type of element (such as a local variable),
  367. /// it is okay to style it as its more general variant (a variable).
  368. ///
  369. /// For tags that extend some parent tag, the documentation links to
  370. /// the parent.
  371. const tags = {
  372. /// A comment.
  373. comment,
  374. /// A line [comment](#highlight.tags.comment).
  375. lineComment: t(comment),
  376. /// A block [comment](#highlight.tags.comment).
  377. blockComment: t(comment),
  378. /// A documentation [comment](#highlight.tags.comment).
  379. docComment: t(comment),
  380. /// Any kind of identifier.
  381. name,
  382. /// The [name](#highlight.tags.name) of a variable.
  383. variableName: t(name),
  384. /// A type [name](#highlight.tags.name).
  385. typeName: typeName,
  386. /// A tag name (subtag of [`typeName`](#highlight.tags.typeName)).
  387. tagName: t(typeName),
  388. /// A property or field [name](#highlight.tags.name).
  389. propertyName: propertyName,
  390. /// An attribute name (subtag of [`propertyName`](#highlight.tags.propertyName)).
  391. attributeName: t(propertyName),
  392. /// The [name](#highlight.tags.name) of a class.
  393. className: t(name),
  394. /// A label [name](#highlight.tags.name).
  395. labelName: t(name),
  396. /// A namespace [name](#highlight.tags.name).
  397. namespace: t(name),
  398. /// The [name](#highlight.tags.name) of a macro.
  399. macroName: t(name),
  400. /// A literal value.
  401. literal,
  402. /// A string [literal](#highlight.tags.literal).
  403. string,
  404. /// A documentation [string](#highlight.tags.string).
  405. docString: t(string),
  406. /// A character literal (subtag of [string](#highlight.tags.string)).
  407. character: t(string),
  408. /// An attribute value (subtag of [string](#highlight.tags.string)).
  409. attributeValue: t(string),
  410. /// A number [literal](#highlight.tags.literal).
  411. number,
  412. /// An integer [number](#highlight.tags.number) literal.
  413. integer: t(number),
  414. /// A floating-point [number](#highlight.tags.number) literal.
  415. float: t(number),
  416. /// A boolean [literal](#highlight.tags.literal).
  417. bool: t(literal),
  418. /// Regular expression [literal](#highlight.tags.literal).
  419. regexp: t(literal),
  420. /// An escape [literal](#highlight.tags.literal), for example a
  421. /// backslash escape in a string.
  422. escape: t(literal),
  423. /// A color [literal](#highlight.tags.literal).
  424. color: t(literal),
  425. /// A URL [literal](#highlight.tags.literal).
  426. url: t(literal),
  427. /// A language keyword.
  428. keyword,
  429. /// The [keyword](#highlight.tags.keyword) for the self or this
  430. /// object.
  431. self: t(keyword),
  432. /// The [keyword](#highlight.tags.keyword) for null.
  433. null: t(keyword),
  434. /// A [keyword](#highlight.tags.keyword) denoting some atomic value.
  435. atom: t(keyword),
  436. /// A [keyword](#highlight.tags.keyword) that represents a unit.
  437. unit: t(keyword),
  438. /// A modifier [keyword](#highlight.tags.keyword).
  439. modifier: t(keyword),
  440. /// A [keyword](#highlight.tags.keyword) that acts as an operator.
  441. operatorKeyword: t(keyword),
  442. /// A control-flow related [keyword](#highlight.tags.keyword).
  443. controlKeyword: t(keyword),
  444. /// A [keyword](#highlight.tags.keyword) that defines something.
  445. definitionKeyword: t(keyword),
  446. /// A [keyword](#highlight.tags.keyword) related to defining or
  447. /// interfacing with modules.
  448. moduleKeyword: t(keyword),
  449. /// An operator.
  450. operator,
  451. /// An [operator](#highlight.tags.operator) that defines something.
  452. derefOperator: t(operator),
  453. /// Arithmetic-related [operator](#highlight.tags.operator).
  454. arithmeticOperator: t(operator),
  455. /// Logical [operator](#highlight.tags.operator).
  456. logicOperator: t(operator),
  457. /// Bit [operator](#highlight.tags.operator).
  458. bitwiseOperator: t(operator),
  459. /// Comparison [operator](#highlight.tags.operator).
  460. compareOperator: t(operator),
  461. /// [Operator](#highlight.tags.operator) that updates its operand.
  462. updateOperator: t(operator),
  463. /// [Operator](#highlight.tags.operator) that defines something.
  464. definitionOperator: t(operator),
  465. /// Type-related [operator](#highlight.tags.operator).
  466. typeOperator: t(operator),
  467. /// Control-flow [operator](#highlight.tags.operator).
  468. controlOperator: t(operator),
  469. /// Program or markup punctuation.
  470. punctuation,
  471. /// [Punctuation](#highlight.tags.punctuation) that separates
  472. /// things.
  473. separator: t(punctuation),
  474. /// Bracket-style [punctuation](#highlight.tags.punctuation).
  475. bracket,
  476. /// Angle [brackets](#highlight.tags.bracket) (usually `<` and `>`
  477. /// tokens).
  478. angleBracket: t(bracket),
  479. /// Square [brackets](#highlight.tags.bracket) (usually `[` and `]`
  480. /// tokens).
  481. squareBracket: t(bracket),
  482. /// Parentheses (usually `(` and `)` tokens). Subtag of
  483. /// [bracket](#highlight.tags.bracket).
  484. paren: t(bracket),
  485. /// Braces (usually `{` and `}` tokens). Subtag of
  486. /// [bracket](#highlight.tags.bracket).
  487. brace: t(bracket),
  488. /// Content, for example plain text in XML or markup documents.
  489. content,
  490. /// [Content](#highlight.tags.content) that represents a heading.
  491. heading,
  492. /// A level 1 [heading](#highlight.tags.heading).
  493. heading1: t(heading),
  494. /// A level 2 [heading](#highlight.tags.heading).
  495. heading2: t(heading),
  496. /// A level 3 [heading](#highlight.tags.heading).
  497. heading3: t(heading),
  498. /// A level 4 [heading](#highlight.tags.heading).
  499. heading4: t(heading),
  500. /// A level 5 [heading](#highlight.tags.heading).
  501. heading5: t(heading),
  502. /// A level 6 [heading](#highlight.tags.heading).
  503. heading6: t(heading),
  504. /// A prose separator (such as a horizontal rule).
  505. contentSeparator: t(content),
  506. /// [Content](#highlight.tags.content) that represents a list.
  507. list: t(content),
  508. /// [Content](#highlight.tags.content) that represents a quote.
  509. quote: t(content),
  510. /// [Content](#highlight.tags.content) that is emphasized.
  511. emphasis: t(content),
  512. /// [Content](#highlight.tags.content) that is styled strong.
  513. strong: t(content),
  514. /// [Content](#highlight.tags.content) that is part of a link.
  515. link: t(content),
  516. /// [Content](#highlight.tags.content) that is styled as code or
  517. /// monospace.
  518. monospace: t(content),
  519. /// [Content](#highlight.tags.content) that has a strike-through
  520. /// style.
  521. strikethrough: t(content),
  522. /// Inserted text in a change-tracking format.
  523. inserted: t(),
  524. /// Deleted text.
  525. deleted: t(),
  526. /// Changed text.
  527. changed: t(),
  528. /// An invalid or unsyntactic element.
  529. invalid: t(),
  530. /// Metadata or meta-instruction.
  531. meta,
  532. /// [Metadata](#highlight.tags.meta) that applies to the entire
  533. /// document.
  534. documentMeta: t(meta),
  535. /// [Metadata](#highlight.tags.meta) that annotates or adds
  536. /// attributes to a given syntactic element.
  537. annotation: t(meta),
  538. /// Processing instruction or preprocessor directive. Subtag of
  539. /// [meta](#highlight.tags.meta).
  540. processingInstruction: t(meta),
  541. /// [Modifier](#highlight.Tag^defineModifier) that indicates that a
  542. /// given element is being defined. Expected to be used with the
  543. /// various [name](#highlight.tags.name) tags.
  544. definition: Tag.defineModifier(),
  545. /// [Modifier](#highlight.Tag^defineModifier) that indicates that
  546. /// something is constant. Mostly expected to be used with
  547. /// [variable names](#highlight.tags.variableName).
  548. constant: Tag.defineModifier(),
  549. /// [Modifier](#highlight.Tag^defineModifier) used to indicate that
  550. /// a [variable](#highlight.tags.variableName) or [property
  551. /// name](#highlight.tags.propertyName) is being called or defined
  552. /// as a function.
  553. function: Tag.defineModifier(),
  554. /// [Modifier](#highlight.Tag^defineModifier) that can be applied to
  555. /// [names](#highlight.tags.name) to indicate that they belong to
  556. /// the language's standard environment.
  557. standard: Tag.defineModifier(),
  558. /// [Modifier](#highlight.Tag^defineModifier) that indicates a given
  559. /// [names](#highlight.tags.name) is local to some scope.
  560. local: Tag.defineModifier(),
  561. /// A generic variant [modifier](#highlight.Tag^defineModifier) that
  562. /// can be used to tag language-specific alternative variants of
  563. /// some common tag. It is recommended for themes to define special
  564. /// forms of at least the [string](#highlight.tags.string) and
  565. /// [variable name](#highlight.tags.variableName) tags, since those
  566. /// come up a lot.
  567. special: Tag.defineModifier()
  568. };
  569. /// This is a highlighter that adds stable, predictable classes to
  570. /// tokens, for styling with external CSS.
  571. ///
  572. /// The following tags are mapped to their name prefixed with `"tok-"`
  573. /// (for example `"tok-comment"`):
  574. ///
  575. /// * [`link`](#highlight.tags.link)
  576. /// * [`heading`](#highlight.tags.heading)
  577. /// * [`emphasis`](#highlight.tags.emphasis)
  578. /// * [`strong`](#highlight.tags.strong)
  579. /// * [`keyword`](#highlight.tags.keyword)
  580. /// * [`atom`](#highlight.tags.atom)
  581. /// * [`bool`](#highlight.tags.bool)
  582. /// * [`url`](#highlight.tags.url)
  583. /// * [`labelName`](#highlight.tags.labelName)
  584. /// * [`inserted`](#highlight.tags.inserted)
  585. /// * [`deleted`](#highlight.tags.deleted)
  586. /// * [`literal`](#highlight.tags.literal)
  587. /// * [`string`](#highlight.tags.string)
  588. /// * [`number`](#highlight.tags.number)
  589. /// * [`variableName`](#highlight.tags.variableName)
  590. /// * [`typeName`](#highlight.tags.typeName)
  591. /// * [`namespace`](#highlight.tags.namespace)
  592. /// * [`className`](#highlight.tags.className)
  593. /// * [`macroName`](#highlight.tags.macroName)
  594. /// * [`propertyName`](#highlight.tags.propertyName)
  595. /// * [`operator`](#highlight.tags.operator)
  596. /// * [`comment`](#highlight.tags.comment)
  597. /// * [`meta`](#highlight.tags.meta)
  598. /// * [`punctuation`](#highlight.tags.punctuation)
  599. /// * [`invalid`](#highlight.tags.invalid)
  600. ///
  601. /// In addition, these mappings are provided:
  602. ///
  603. /// * [`regexp`](#highlight.tags.regexp),
  604. /// [`escape`](#highlight.tags.escape), and
  605. /// [`special`](#highlight.tags.special)[`(string)`](#highlight.tags.string)
  606. /// are mapped to `"tok-string2"`
  607. /// * [`special`](#highlight.tags.special)[`(variableName)`](#highlight.tags.variableName)
  608. /// to `"tok-variableName2"`
  609. /// * [`local`](#highlight.tags.local)[`(variableName)`](#highlight.tags.variableName)
  610. /// to `"tok-variableName tok-local"`
  611. /// * [`definition`](#highlight.tags.definition)[`(variableName)`](#highlight.tags.variableName)
  612. /// to `"tok-variableName tok-definition"`
  613. /// * [`definition`](#highlight.tags.definition)[`(propertyName)`](#highlight.tags.propertyName)
  614. /// to `"tok-propertyName tok-definition"`
  615. const classHighlighter = tagHighlighter([
  616. { tag: tags.link, class: "tok-link" },
  617. { tag: tags.heading, class: "tok-heading" },
  618. { tag: tags.emphasis, class: "tok-emphasis" },
  619. { tag: tags.strong, class: "tok-strong" },
  620. { tag: tags.keyword, class: "tok-keyword" },
  621. { tag: tags.atom, class: "tok-atom" },
  622. { tag: tags.bool, class: "tok-bool" },
  623. { tag: tags.url, class: "tok-url" },
  624. { tag: tags.labelName, class: "tok-labelName" },
  625. { tag: tags.inserted, class: "tok-inserted" },
  626. { tag: tags.deleted, class: "tok-deleted" },
  627. { tag: tags.literal, class: "tok-literal" },
  628. { tag: tags.string, class: "tok-string" },
  629. { tag: tags.number, class: "tok-number" },
  630. { tag: [tags.regexp, tags.escape, tags.special(tags.string)], class: "tok-string2" },
  631. { tag: tags.variableName, class: "tok-variableName" },
  632. { tag: tags.local(tags.variableName), class: "tok-variableName tok-local" },
  633. { tag: tags.definition(tags.variableName), class: "tok-variableName tok-definition" },
  634. { tag: tags.special(tags.variableName), class: "tok-variableName2" },
  635. { tag: tags.definition(tags.propertyName), class: "tok-propertyName tok-definition" },
  636. { tag: tags.typeName, class: "tok-typeName" },
  637. { tag: tags.namespace, class: "tok-namespace" },
  638. { tag: tags.className, class: "tok-className" },
  639. { tag: tags.macroName, class: "tok-macroName" },
  640. { tag: tags.propertyName, class: "tok-propertyName" },
  641. { tag: tags.operator, class: "tok-operator" },
  642. { tag: tags.comment, class: "tok-comment" },
  643. { tag: tags.meta, class: "tok-meta" },
  644. { tag: tags.invalid, class: "tok-invalid" },
  645. { tag: tags.punctuation, class: "tok-punctuation" }
  646. ]);
  647. export { Tag, classHighlighter, highlightTree, styleTags, tagHighlighter, tags };