UNPKG

@mui/internal-docs-infra

Version:

MUI Infra - internal documentation creation tools.

52 lines (51 loc) 1.86 kB
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime"; import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; import { createStarryNight } from '@wooorm/starry-night'; import { grammars, extensionMap } from "./grammars.js"; import { starryNightGutter } from "./addLineGutters.js"; var starryNight = null; export var parseSource = function parseSource(source, fileName) { if (!starryNight) { throw new Error('Starry Night not initialized. Use createParseSource to create an initialized parseSource function.'); } var fileType = fileName.slice(fileName.lastIndexOf('.')); if (!extensionMap[fileType]) { // Return a basic HAST root node with the source text for unsupported file types // TODO: should we split and add line gutters? return { type: 'root', children: [{ type: 'text', value: source }] }; } var highlighted = starryNight.highlight(source, extensionMap[fileType]); starryNightGutter(highlighted); // mutates the tree to add line gutters return highlighted; }; export var createParseSource = /*#__PURE__*/function () { var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() { return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: if (starryNight) { _context.next = 4; break; } _context.next = 3; return createStarryNight(grammars); case 3: starryNight = _context.sent; case 4: return _context.abrupt("return", parseSource); case 5: case "end": return _context.stop(); } }, _callee); })); return function createParseSource() { return _ref.apply(this, arguments); }; }();