Java Code Examples for io.noties.markwon.Markwon#create()

The following examples show how to use io.noties.markwon.Markwon#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MarkwonEditorImplTest.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Test
public void process() {
    // create markwon
    final Markwon markwon = Markwon.create(RuntimeEnvironment.application);

    // default punctuation
    final MarkwonEditor editor = MarkwonEditor.create(markwon);

    final SpannableStringBuilder builder = new SpannableStringBuilder("**bold**");

    editor.process(builder);

    final PunctuationSpan[] spans = builder.getSpans(0, builder.length(), PunctuationSpan.class);
    assertEquals(2, spans.length);

    final PunctuationSpan first = spans[0];
    assertEquals(0, builder.getSpanStart(first));
    assertEquals(2, builder.getSpanEnd(first));

    final PunctuationSpan second = spans[1];
    assertEquals(6, builder.getSpanStart(second));
    assertEquals(8, builder.getSpanEnd(second));
}
 
Example 2
Source File: MainActivity.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // obtain an instance of Markwon
    // here we are creating as core markwon (no additional plugins are registered)
    final Markwon markwon = Markwon.create(this);

    final Adapt adapt = Adapt.create();

    final List<Item> items = new ArrayList<>();
    final SampleItem.OnClickListener onClickListener = this::showSample;
    for (Sample sample : Sample.values()) {
        items.add(new SampleItem(sample, markwon, onClickListener));
    }
    adapt.setItems(items);

    final RecyclerView recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    recyclerView.addItemDecoration(createSampleItemDecoration());
    recyclerView.setAdapter(adapt);
}
 
Example 3
Source File: EditorActivity.java    From Markwon with Apache License 2.0 5 votes vote down vote up
private void simple_process() {
    // Process highlight in-place (right after text has changed)

    // obtain Markwon instance
    final Markwon markwon = Markwon.create(this);

    // create editor
    final MarkwonEditor editor = MarkwonEditor.create(markwon);

    // set edit listener
    editText.addTextChangedListener(MarkwonEditorTextWatcher.withProcess(editor));
}
 
Example 4
Source File: EditorActivity.java    From Markwon with Apache License 2.0 5 votes vote down vote up
private void simple_pre_render() {
    // Process highlight in background thread

    final Markwon markwon = Markwon.create(this);
    final MarkwonEditor editor = MarkwonEditor.create(markwon);

    editText.addTextChangedListener(MarkwonEditorTextWatcher.withPreRender(
            editor,
            Executors.newCachedThreadPool(),
            editText));
}
 
Example 5
Source File: EditorActivity.java    From Markwon with Apache License 2.0 5 votes vote down vote up
private void heading() {
    final Markwon markwon = Markwon.create(this);
    final MarkwonEditor editor = MarkwonEditor.builder(markwon)
            .useEditHandler(new HeadingEditHandler())
            .build();

    editText.addTextChangedListener(MarkwonEditorTextWatcher.withPreRender(
            editor, Executors.newSingleThreadExecutor(), editText));
}
 
Example 6
Source File: CoreActivity.java    From Markwon with Apache License 2.0 5 votes vote down vote up
/**
 * To simply apply raw (non-parsed) markdown call {@link Markwon#setMarkdown(TextView, String)}
 */
private void simple() {

    // this is raw markdown
    final String markdown = "Hello **markdown**!";

    final Markwon markwon = Markwon.create(this);

    // this will parse raw markdown and set parsed content to specified TextView
    markwon.setMarkdown(textView, markdown);
}
 
Example 7
Source File: EditorActivity.java    From Markwon with Apache License 2.0 4 votes vote down vote up
private void newLine() {
    final Markwon markwon = Markwon.create(this);
    final MarkwonEditor editor = MarkwonEditor.create(markwon);
    final TextWatcher textWatcher = MarkdownNewLine.wrap(MarkwonEditorTextWatcher.withProcess(editor));
    editText.addTextChangedListener(textWatcher);
}
 
Example 8
Source File: CoreActivity.java    From Markwon with Apache License 2.0 4 votes vote down vote up
/**
 * To apply already parsed markdown use {@link Markwon#setParsedMarkdown(TextView, Spanned)}
 */
private void alreadyParsed() {

    final String markdown = "This **is** pre-parsed [markdown](#)";

    final Markwon markwon = Markwon.create(this);

    // parse markdown to obtain a Node
    final Node node = markwon.parse(markdown);

    // create a spanned content from parsed node
    final Spanned spanned = markwon.render(node);

    // apply parsed markdown
    markwon.setParsedMarkdown(textView, spanned);
}
 
Example 9
Source File: CoreActivity.java    From Markwon with Apache License 2.0 3 votes vote down vote up
/**
 * Create a simple instance of Markwon with only Core plugin registered
 * this will handle all _natively_ supported by commonmark-java nodes:
 * <ul>
 * <li>StrongEmphasis</li>
 * <li>Emphasis</li>
 * <li>BlockQuote</li>
 * <li>Code</li>
 * <li>FencedCodeBlock</li>
 * <li>IndentedCodeBlock</li>
 * <li>ListItem (bullet-list and ordered list</li>
 * <li>Heading</li>
 * <li>Link</li>
 * <li>ThematicBreak</li>
 * <li>Paragraph (please note that there is no default span for a paragraph registered)</li>
 * </ul>
 * <p>
 * and basic core functionality:
 * <ul>
 * <li>Append text</li>
 * <li>Insert new lines (soft and hard breaks)</li>
 * </ul>
 */
private void step_1() {

    // short call
    final Markwon markwon = Markwon.create(this);

    // this is the same as calling
    final Markwon markwon2 = Markwon.builder(this)
            .usePlugin(CorePlugin.create())
            .build();
}
 
Example 10
Source File: CoreActivity.java    From Markwon with Apache License 2.0 3 votes vote down vote up
/**
 * To apply markdown in a different context (other than textView) use {@link Markwon#toMarkdown(String)}
 * <p>
 * Please note that some features won't work unless they are used in a TextView context. For example
 * there might be misplaced ordered lists (ordered list must have TextPaint in order to properly measure
 * its number). But also images and tables (they belong to independent modules now). Images and tables
 * are using some work-arounds in order to be displayed in relatively limited context without proper way
 * of invalidation. But if a Toast for example is created with a custom view
 * ({@code new Toast(this).setView(...) }) and has access to a TextView everything <em>should</em> work.
 */
private void toast() {

    final String markdown = "*Toast* __here__!\n\n> And a quote!";

    final Markwon markwon = Markwon.create(this);

    final Spanned spanned = markwon.toMarkdown(markdown);

    Toast.makeText(this, spanned, Toast.LENGTH_LONG).show();
}