astro-tokenizer.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. const JsxLikeTokenizer = require("./jsx-like-tokenizer");
  3. const MINUS = "-".charCodeAt(0);
  4. const LINE_FEED = "\n".charCodeAt(0);
  5. module.exports = class AstroTokenizer extends JsxLikeTokenizer {
  6. constructor(...args) {
  7. super(...args);
  8. this._hasFrontmatter = false;
  9. }
  10. parse() {
  11. super.parse();
  12. }
  13. stateText(c) {
  14. if (!this._hasFrontmatter) {
  15. if (c === MINUS) {
  16. const startIndex = this.index - this.offset;
  17. if (
  18. startIndex === 0 &&
  19. this.buffer.slice(startIndex, startIndex + 3) === "---"
  20. ) {
  21. const closeIndex = this.buffer.indexOf("\n---", startIndex + 3);
  22. if (closeIndex >= 0) {
  23. this.index = closeIndex + 3;
  24. this._hasFrontmatter = true;
  25. return;
  26. }
  27. }
  28. }
  29. if (c === LINE_FEED) {
  30. const startIndex = this.index - this.offset;
  31. if (this.buffer.slice(startIndex, startIndex + 4) === "\n---") {
  32. const closeIndex = this.buffer.indexOf("\n---", startIndex + 4);
  33. if (closeIndex >= 0) {
  34. this.index = closeIndex + 3;
  35. this._hasFrontmatter = true;
  36. return;
  37. }
  38. }
  39. }
  40. }
  41. super.stateText(c);
  42. }
  43. };