Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Astro mode syntax highlighting #5269

Merged
merged 7 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions demo/kitchen-sink/docs/astro.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
const visible = true;
const name = "Legend Sabbir"
---
{visible && <p>Show me!</p>}

{visible ? <p>Show me!</p> : <p>Else show me!</p>}

<h1>{name}</h1>
1 change: 1 addition & 0 deletions src/ext/modelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
20 changes: 20 additions & 0 deletions src/mode/astro.js
Original file line number Diff line number Diff line change
@@ -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;
100 changes: 100 additions & 0 deletions src/mode/astro_highlight_rules.js
Original file line number Diff line number Diff line change
@@ -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;