snippet.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.snippetPlugin = void 0;
  7. const fs_1 = __importDefault(require("fs"));
  8. exports.snippetPlugin = (md, root) => {
  9. const parser = (state, startLine, endLine, silent) => {
  10. const CH = '<'.charCodeAt(0);
  11. const pos = state.bMarks[startLine] + state.tShift[startLine];
  12. const max = state.eMarks[startLine];
  13. // if it's indented more than 3 spaces, it should be a code block
  14. if (state.sCount[startLine] - state.blkIndent >= 4) {
  15. return false;
  16. }
  17. for (let i = 0; i < 3; ++i) {
  18. const ch = state.src.charCodeAt(pos + i);
  19. if (ch !== CH || pos + i >= max)
  20. return false;
  21. }
  22. if (silent) {
  23. return true;
  24. }
  25. const start = pos + 3;
  26. const end = state.skipSpacesBack(max, pos);
  27. const rawPath = state.src.slice(start, end).trim().replace(/^@/, root);
  28. const filename = rawPath.split(/{/).shift().trim();
  29. const content = fs_1.default.existsSync(filename)
  30. ? fs_1.default.readFileSync(filename).toString()
  31. : 'Not found: ' + filename;
  32. const meta = rawPath.replace(filename, '');
  33. state.line = startLine + 1;
  34. const token = state.push('fence', 'code', 0);
  35. token.info = filename.split('.').pop() + meta;
  36. token.content = content;
  37. token.markup = '```';
  38. token.map = [startLine, startLine + 1];
  39. return true;
  40. };
  41. md.block.ruler.before('fence', 'snippet', parser);
  42. };
  43. //# sourceMappingURL=snippet.js.map