file-md.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const source_map_1 = require("@volar/source-map");
  4. const parseSfc_1 = require("../utils/parseSfc");
  5. const codeblockReg = /```[\s\S]+?```/g;
  6. const inlineCodeblockReg = /`[^\n`]+?`/g;
  7. const scriptSetupReg = /\\\<[\s\S]+?\>\n?/g;
  8. const sfcBlockReg = /\<(script|style)\b[\s\S]*?\>([\s\S]*?)\<\/\1\>/g;
  9. const angleBracketReg = /\<\S*\:\S*\>/g;
  10. const linkReg = /\[[\s\S]*?\]\([\s\S]*?\)/g;
  11. const codeSnippetImportReg = /^\s*<<<\s*.+/gm;
  12. const plugin = () => {
  13. return {
  14. version: 1,
  15. parseSFC(fileName, content) {
  16. if (fileName.endsWith('.md')) {
  17. content = content
  18. // code block
  19. .replace(codeblockReg, match => '```' + ' '.repeat(match.length - 6) + '```')
  20. // inline code block
  21. .replace(inlineCodeblockReg, match => `\`${' '.repeat(match.length - 2)}\``)
  22. // # \<script setup>
  23. .replace(scriptSetupReg, match => ' '.repeat(match.length))
  24. // <<< https://vitepress.dev/guide/markdown#import-code-snippets
  25. .replace(codeSnippetImportReg, match => ' '.repeat(match.length));
  26. const codes = [];
  27. for (const match of content.matchAll(sfcBlockReg)) {
  28. if (match.index !== undefined) {
  29. const matchText = match[0];
  30. codes.push([matchText, undefined, match.index]);
  31. codes.push('\n\n');
  32. content = content.substring(0, match.index) + ' '.repeat(matchText.length) + content.substring(match.index + matchText.length);
  33. }
  34. }
  35. content = content
  36. // angle bracket: <http://foo.com>
  37. .replace(angleBracketReg, match => ' '.repeat(match.length))
  38. // [foo](http://foo.com)
  39. .replace(linkReg, match => ' '.repeat(match.length));
  40. codes.push('<template>\n');
  41. codes.push([content, undefined, 0]);
  42. codes.push('\n</template>');
  43. const file2VueSourceMap = new source_map_1.SourceMap((0, source_map_1.buildMappings)(codes));
  44. const sfc = (0, parseSfc_1.parse)((0, source_map_1.toString)(codes));
  45. if (sfc.descriptor.template) {
  46. transformRange(sfc.descriptor.template);
  47. }
  48. if (sfc.descriptor.script) {
  49. transformRange(sfc.descriptor.script);
  50. }
  51. if (sfc.descriptor.scriptSetup) {
  52. transformRange(sfc.descriptor.scriptSetup);
  53. }
  54. for (const style of sfc.descriptor.styles) {
  55. transformRange(style);
  56. }
  57. for (const customBlock of sfc.descriptor.customBlocks) {
  58. transformRange(customBlock);
  59. }
  60. return sfc;
  61. function transformRange(block) {
  62. block.loc.start.offset = file2VueSourceMap.toSourceOffset(block.loc.start.offset)?.[0] ?? -1;
  63. block.loc.end.offset = file2VueSourceMap.toSourceOffset(block.loc.end.offset)?.[0] ?? -1;
  64. }
  65. }
  66. ;
  67. }
  68. };
  69. };
  70. exports.default = plugin;
  71. //# sourceMappingURL=file-md.js.map