Skip to content

Commit

Permalink
strip all ansi escapes in preview mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilia Pozdnyakov committed Mar 1, 2023
1 parent 76b561a commit de07614
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.5

- Strip all ANSI escapes in preview mode, not just SGR ones

## 1.1.4

- Cleaned up extension activation events
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "iliazeus",
"displayName": "ANSI Colors",
"description": "ANSI color styling for text documents",
"version": "1.1.4",
"version": "1.1.5",
"license": "MIT",
"repository": {
"url": "https://github.com/iliazeus/vscode-ansi"
Expand Down
46 changes: 15 additions & 31 deletions src/ansi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,50 +186,34 @@ export class Parser {
continue;
}

const mOffset = text.indexOf("m", index + 2);
if (mOffset === -1) {
const match = text.slice(index + 2).match(/^([0-9;]*)([a-zA-Z])/);
if (!match) {
index += 1;
continue;
}

const argString = text.substring(index + 2, mOffset);

if (!/^[0-9;]*$/.test(argString)) {
const firstLetterOffset = argString.search(/[a-zA-Z]/);

if (firstLetterOffset < 0) {
index += 1;
continue;
}

spans.push({
...style,
offset: index,
length: 2 + firstLetterOffset + 1,
attributeFlags: style.attributeFlags | AttributeFlags.EscapeSequence,
});

index += 2 + firstLetterOffset + 1;
continue;
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const argString = match[1]!, commandLetter = match[2]!;

spans.push({
...style,
offset: index,
length: mOffset - index + 1,
length: 2 + argString.length + 1,
attributeFlags: style.attributeFlags | AttributeFlags.EscapeSequence,
});

const args = argString
.split(";")
.filter((arg) => arg !== "")
.map((arg) => parseInt(arg, 10));
if (args.length === 0) args.push(0);
if (commandLetter === "m") {
const args = argString
.split(";")
.filter((arg) => arg !== "")
.map((arg) => parseInt(arg, 10));
if (args.length === 0) args.push(0);

this._applyCodes(args, style);
this._applyCodes(args, style);
}

textOffset = mOffset + 1;
index = mOffset + 1;
textOffset = index + 2 + argString.length + 1;
index = textOffset;
}

spans.push({ ...style, offset: textOffset, length: index - textOffset });
Expand Down

0 comments on commit de07614

Please sign in to comment.