NodeToHTML

View as Markdown

NodeToHTML

NodeToHTML converts one text node of a JSON Rich Text Editor (JSON RTE) field to HTML. A text node is the leaf of a JSON RTE document. It is a JSON object with a text key, plus one key for each formatting mark such as bold. Use this class to

  • write the text of one node and its marks as HTML
  • check the HTML that an Option writes for each mark

NodeToHTML has one static method and no instances. Its constructor is private, so call textNodeToHTML on the class itself.

textNodeToHTML

textNodeToHTML converts one JSON Rich Text Editor text node to HTML and applies each of its formatting marks through an Option.

NameTypeDescription
nodeText (required)JSONObject

Text node with a text key and mark keys.

Default: Not applicable
renderOption (required)Option

Renderer that writes each mark, usually a DefaultOption.

Default: Not applicable

Returns: String. The text node as HTML.

Validation

  • There is no client-side validation.
  • textNodeToHTML throws NullPointerException when nodeText is null, and when renderOption is null and the node has at least one mark key.
  • A node without a text key returns an empty string, plus the HTML of any marks it has.

Behavior

  • Client-side only. textNodeToHTML reads the JSON you pass and makes no request.
  • textNodeToHTML first replaces each line break (\n) in the text with <br />.
  • It then checks the mark keys in this order and calls renderOption.renderMark once for each key the node has: superscript, subscript, inlineCode, strikethrough, underline, italic, bold. A later mark wraps an earlier one.
  • For the break key, it applies the break mark only when the text holds no <br /> after the line-break replacement.
  • textNodeToHTML checks only that a mark key exists, not its value. A node with "bold": false still renders as bold.
  • textNodeToHTML does not escape <, > or & in the text, so the HTML treats those characters as markup.
  • See DefaultOption renderMark for the HTML tag that the SDK's implementation writes for each mark.

Example

Basic usage: convert a text node with two marks

import android.util.Log;

import com.contentstack.sdk.DefaultOption;
import com.contentstack.sdk.NodeToHTML;

import org.json.JSONException;
import org.json.JSONObject;

try {
    JSONObject textNode = new JSONObject()
            .put("text", "Summer sale")
            .put("bold", true)
            .put("italic", true);
    String html = NodeToHTML.textNodeToHTML(textNode, new DefaultOption());
    Log.d("Contentstack", html); // <strong><em>Summer sale</em></strong>
} catch (JSONException e) {
    Log.e("Contentstack", e.getMessage());
}

Edge case: line breaks and a mark set to false

import android.util.Log;

import com.contentstack.sdk.DefaultOption;
import com.contentstack.sdk.NodeToHTML;

import org.json.JSONException;
import org.json.JSONObject;

try {
    DefaultOption option = new DefaultOption();

    JSONObject twoLines = new JSONObject().put("text", "Line one\nLine two");
    Log.d("Contentstack", NodeToHTML.textNodeToHTML(twoLines, option)); // Line one<br />Line two

    // The bold key exists, so the text renders as bold even though its value is false
    JSONObject notBold = new JSONObject().put("text", "Regular").put("bold", false);
    Log.d("Contentstack", NodeToHTML.textNodeToHTML(notBold, option)); // <strong>Regular</strong>
} catch (JSONException e) {
    Log.e("Contentstack", e.getMessage());
}