Skip to content

Commit

Permalink
\ will now negate the Color Markdown
Browse files Browse the repository at this point in the history
* Compatibility with everything but Hyperlink
  • Loading branch information
MeAlam1 committed Dec 10, 2024
1 parent b5070a0 commit 54db08c
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions common/src/main/java/software/bluelib/markdown/syntax/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public MutableComponent applyColor(MutableComponent pComponent) {

Pattern pattern = Pattern.compile(
prefix + "(#?[0-9A-Fa-f]{6}|\\d{1,3}(?:,\\d{1,3}){2,3})" + suffix + "\\((.*?)\\)");

for (Component sibling : pComponent.getSiblings()) {
BaseLogger.log(BaseLogLevel.INFO, "Processing sibling: " + sibling.getString(), true);

Expand All @@ -106,8 +107,21 @@ public MutableComponent applyColor(MutableComponent pComponent) {
MutableComponent styledSibling = Component.empty();

int lastIndex = 0;
boolean foundValidMatch = false;

while (matcher.find()) {
String beforeMatch = siblingText.substring(lastIndex, matcher.start());

if (beforeMatch.endsWith("\\")) {
BaseLogger.log(BaseLogLevel.INFO, "Escape sequence detected before prefix. Skipping markdown application.", true);

styledSibling.append(Component.literal(beforeMatch.substring(0, beforeMatch.length() - 1)));

styledSibling.append(Component.literal(matcher.group(0)));

lastIndex = matcher.end();
continue;
}

String color = matcher.group(1).trim();
String text = matcher.group(2).trim();

Expand All @@ -126,27 +140,22 @@ public MutableComponent applyColor(MutableComponent pComponent) {
BaseLogger.log(BaseLogLevel.INFO, "Appending styled text: " + text, true);
styledSibling.append(coloredText);

foundValidMatch = true;
lastIndex = matcher.end();
} else {
BaseLogger.log(BaseLogLevel.WARNING, "Invalid color: " + color, true);
BaseLogger.log(BaseLogLevel.WARNING, "Returning original component due to invalid color.", true);
return pComponent;
}

lastIndex = matcher.end();
}

if (!foundValidMatch) {
BaseLogger.log(BaseLogLevel.INFO, "No valid matches found. Appending sibling as-is: " + siblingText, true);
result.append(sibling);
} else {
String remainingText = siblingText.substring(lastIndex);
String remainingText = siblingText.substring(lastIndex);
if (!remainingText.isEmpty()) {
BaseLogger.log(BaseLogLevel.INFO, "Appending remaining text: " + remainingText, true);
styledSibling.append(Component.literal(remainingText));

BaseLogger.log(BaseLogLevel.INFO, "Final styled sibling: " + styledSibling.getString(), true);
result.append(styledSibling);
}

BaseLogger.log(BaseLogLevel.INFO, "Final styled sibling: " + styledSibling.getString(), true);
result.append(styledSibling);
} else {
BaseLogger.log(BaseLogLevel.INFO, "Sibling is not mutable. Appending as-is: " + sibling.getString(), true);
result.append(sibling);
Expand All @@ -157,6 +166,7 @@ public MutableComponent applyColor(MutableComponent pComponent) {
return result;
}


/**
* Overrides the {@link MarkdownFeature#applyFormat(String)} method to apply the formatting logic.
* <p>
Expand Down

0 comments on commit 54db08c

Please sign in to comment.