From 55bb6541795781abcb96cbc8e34441830fcd8919 Mon Sep 17 00:00:00 2001
From: Legend Sabbir <123853512+legendSabbir@users.noreply.github.com>
Date: Fri, 13 Oct 2023 17:45:26 +0600
Subject: [PATCH] Astro mode syntax highlighting (#5269)
* Add files via upload
* added astro mode
* added astro mode
* added astro mode
* Delete astro.js
* Delete astro_highlight_rules.js
* Update astro_highlight_rules.js
---
demo/kitchen-sink/docs/astro.astro | 9 +++
src/ext/modelist.js | 1 +
src/mode/astro.js | 20 ++++++
src/mode/astro_highlight_rules.js | 100 +++++++++++++++++++++++++++++
4 files changed, 130 insertions(+)
create mode 100644 demo/kitchen-sink/docs/astro.astro
create mode 100644 src/mode/astro.js
create mode 100644 src/mode/astro_highlight_rules.js
diff --git a/demo/kitchen-sink/docs/astro.astro b/demo/kitchen-sink/docs/astro.astro
new file mode 100644
index 00000000000..7e5437e6029
--- /dev/null
+++ b/demo/kitchen-sink/docs/astro.astro
@@ -0,0 +1,9 @@
+---
+const visible = true;
+const name = "Legend Sabbir"
+---
+{visible &&
Show me!
}
+
+{visible ? Show me!
: Else show me!
}
+
+{name}
\ No newline at end of file
diff --git a/src/ext/modelist.js b/src/ext/modelist.js
index ab47f3b3851..b114c1e7975 100644
--- a/src/ext/modelist.js
+++ b/src/ext/modelist.js
@@ -56,6 +56,7 @@ var supportedModes = {
AsciiDoc: ["asciidoc|adoc"],
ASL: ["dsl|asl|asl.json"],
Assembly_x86:["asm|a"],
+ Astro: ["astro"],
AutoHotKey: ["ahk"],
BatchFile: ["bat|cmd"],
BibTeX: ["bib"],
diff --git a/src/mode/astro.js b/src/mode/astro.js
new file mode 100644
index 00000000000..a6ae6a00967
--- /dev/null
+++ b/src/mode/astro.js
@@ -0,0 +1,20 @@
+"use strict";
+
+var oop = require("../lib/oop");
+var HtmlMode = require("./html").Mode;
+var AstroHighlightRules = require("./astro_highlight_rules").AstroHighlightRules;
+var HtmlBehaviour = require("./behaviour/html").HtmlBehaviour;
+
+var Mode = function() {
+ HtmlMode.call(this);
+ this.HighlightRules = AstroHighlightRules;
+ this.$behaviour = new HtmlBehaviour();
+};
+
+oop.inherits(Mode, HtmlMode);
+
+(function() {
+ this.$id = "ace/mode/astro";
+}).call(Mode.prototype);
+
+exports.Mode = Mode;
\ No newline at end of file
diff --git a/src/mode/astro_highlight_rules.js b/src/mode/astro_highlight_rules.js
new file mode 100644
index 00000000000..0348005de45
--- /dev/null
+++ b/src/mode/astro_highlight_rules.js
@@ -0,0 +1,100 @@
+"use strict";
+
+var oop = require("../lib/oop");
+var HtmlHighlightRules = require("./html_highlight_rules").HtmlHighlightRules;
+var JavascriptHighlightRules = require("./javascript_highlight_rules").JavaScriptHighlightRules;
+
+var AstroHighlightRules = function () {
+ HtmlHighlightRules.call(this);
+
+ var astro = {
+ token: "paren.quasi.start.astro.level3",
+ regex: /{/,
+ next: "inline-js-start"
+ };
+
+ for (var key in this.$rules) {
+ if (key.startsWith("js") || key.startsWith("css") || key.startsWith("comment")) continue;
+ this.$rules[key].unshift(astro);
+ }
+
+ this.$rules.start.unshift({
+ token: "comment",
+ regex: "---",
+ onMatch: function (value, state, stack) {
+ stack.splice(0);
+ return this.token;
+ },
+ next: "js-start"
+ });
+
+ this.embedRules(JavascriptHighlightRules, "js-", [{
+ regex: "---",
+ token: "comment",
+ next: "start",
+ onMatch: function (value, state, stack) {
+ stack.splice(0);
+ return this.token;
+ }
+ }]);
+
+ this.embedRules(JavascriptHighlightRules, "inline-js-", [{
+ regex: /}/,
+ token: "paren.quasi.end.astro.level3",
+ onMatch: function (value, state, stack) {
+ if (stack.length) {
+ if (stack.includes("inline-js-start")) {
+ stack.shift();
+ this.next = stack.shift();
+ if (this.next.indexOf("string") !== -1) return "paren.quasi.end";
+ return "paren.rparen";
+ } else {
+ if (stack.includes("string.attribute-value.xml0")) {
+ this.next = "string.attribute-value.xml0";
+ }
+ else if (stack.includes("tag_stuff")) {
+ this.next = "tag_stuff";
+ }
+ return this.token;
+ }
+ } else {
+ this.next = this.nextState;
+ return this.token;
+ }
+ },
+ nextState: "start"
+ }, {
+ regex: /{/,
+ token: "paren.lparen",
+ push: "inline-js-start"
+ }]);
+
+ var overwriteJSXendRule = function (prefix) {
+ for (var index in this.$rules[prefix + "jsxAttributes"]) {
+ if (this.$rules[prefix + "jsxAttributes"][index].token === "meta.tag.punctuation.tag-close.xml") {
+ this.$rules[prefix + "jsxAttributes"][index].onMatch = function (value, currentState, stack) {
+ if (currentState == stack[0])
+ stack.shift();
+ if (value.length == 2) {
+ if (stack[0] == this.nextState)
+ stack[1]--;
+ if (!stack[1] || stack[1] < 0) {
+ stack.splice(0, 2);
+ }
+ }
+ this.next = stack[0] || prefix + "start";
+ return [{ type: this.token, value: value }];
+ };
+ break;
+ }
+ }
+ };
+
+ overwriteJSXendRule.call(this, "js-");
+ overwriteJSXendRule.call(this, "inline-js-");
+
+ this.normalizeRules();
+};
+
+oop.inherits(AstroHighlightRules, HtmlHighlightRules);
+exports.AstroHighlightRules = AstroHighlightRules;