private void escapeTagContent(final String content, final @Nullable QuotingOverride preference) {
      boolean mustBeQuoted = preference == QuotingOverride.QUOTED;
      boolean hasSingleQuote = false;
      boolean hasDoubleQuote = false;

      for (int i = 0; i < content.length(); i++) {
        final char active = content.charAt(i);
        if (active == TokenParser.TAG_END || active == TokenParser.SEPARATOR || active == ' ') { // space is not technically required here, but is preferred
          mustBeQuoted = true;
          if (hasSingleQuote && hasDoubleQuote) break;
        } else if (active == '\'') {
          hasSingleQuote = true;
          break; // we know our quoting style
        } else if (active == '"') {
          hasDoubleQuote = true;
          if (mustBeQuoted && hasSingleQuote) break;
        }
      }

      if (hasSingleQuote) { // double-quoted
        this.consumer.append('"');
        appendEscaping(this.consumer, content, DOUBLE_QUOTED_ESCAPES, true);
        this.consumer.append('"');
      } else if (hasDoubleQuote || mustBeQuoted) {
        // single-quoted
        this.consumer.append('\'');
        appendEscaping(this.consumer, content, SINGLE_QUOTED_ESCAPES, true);
        this.consumer.append('\'');
      } else { // unquoted
        appendEscaping(this.consumer, content, TAG_TOKENS, false);
      }
    }
mark tag boundaries within the stack, without needing to mess with typing too much.
text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/MiniMessageSerializer.java