org.commonmark.Extension Java Examples

The following examples show how to use org.commonmark.Extension. 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: MarkdownUtil.java    From Roothub with GNU Affero General Public License v3.0 7 votes vote down vote up
/**
 * 渲染 Markdown
 * @param content
 * @return
 */
public static String render(String content) {
    List<Extension> extensions = Arrays.asList(
            AutolinkExtension.create(),
            TablesExtension.create());

    Parser parser = Parser.builder()
            .extensions(extensions)
            .build();
    // 回车一次就可以实现换行
    HtmlRenderer renderer = HtmlRenderer.builder()
            .softbreak("<br/>")
            .attributeProviderFactory(context -> new MyAttributeProvider())
            .extensions(extensions)
            .build();
    Node document = parser.parse(content == null ? "" : content);
    return renderer.render(document);
}
 
Example #2
Source File: MarkdownController.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@PostMapping("/")
public String markdownRenderer(@RequestBody String payload) {
  // Set up HTML renderer
  // https://github.com/atlassian/commonmark-java#extensions
  List<Extension> extensions =
      Arrays.asList(TablesExtension.create(), StrikethroughExtension.create());
  Parser parser = Parser.builder().extensions(extensions).build();
  Node document = parser.parse(payload);
  HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();
  // Convert Markdown to HTML
  String converted = renderer.render(document);

  // Use prepackaged policies to sanitize HTML. Cusomized and tighter standards
  // are recommended.
  PolicyFactory policy =
      Sanitizers.FORMATTING
          .and(Sanitizers.BLOCKS)
          .and(Sanitizers.LINKS)
          .and(Sanitizers.IMAGES)
          .and(Sanitizers.TABLES);
  String safeHtml = policy.sanitize(converted);

  return safeHtml;
}
 
Example #3
Source File: MarkDownProvider.java    From mvvm-template with GNU General Public License v3.0 6 votes vote down vote up
protected static void render(@NonNull TextView textView, String markdown, int width) {
    List<Extension> extensions = Arrays.asList(
            StrikethroughExtension.create(),
            AutolinkExtension.create(),
            TablesExtension.create(),
            InsExtension.create(),
            EmojiExtension.create(),
            MentionExtension.create(),
            YamlFrontMatterExtension.create());
    Parser parser = Parser.builder()
            .extensions(extensions)
            .build();
    try {
        Node node = parser.parse(markdown);
        String rendered = HtmlRenderer
                .builder()
                .extensions(extensions)
                .build()
                .render(node);
        HtmlHelper.htmlIntoTextView(textView, rendered, (width - (textView.getPaddingStart() + textView.getPaddingEnd())));
    } catch (Exception ignored) {
        HtmlHelper.htmlIntoTextView(textView, markdown, (width - (textView.getPaddingStart() + textView.getPaddingEnd())));
    }
}
 
Example #4
Source File: TaleUtils.java    From tale with MIT License 6 votes vote down vote up
/**
 * markdown转换为html
 *
 * @param markdown
 * @return
 */
public static String mdToHtml(String markdown) {
    if (StringKit.isBlank(markdown)) {
        return "";
    }

    List<Extension> extensions = Arrays.asList(TablesExtension.create());
    Parser          parser     = Parser.builder().extensions(extensions).build();
    Node            document   = parser.parse(markdown);
    HtmlRenderer    renderer   = HtmlRenderer.builder().extensions(extensions).build();
    String          content    = renderer.render(document);
    content = Commons.emoji(content);

    // 支持网易云音乐输出
    if (TaleConst.BCONF.getBoolean("app.support_163_music", true) && content.contains("[mp3:")) {
        content = content.replaceAll("\\[mp3:(\\d+)\\]", "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" width=350 height=106 src=\"//music.163.com/outchain/player?type=2&id=$1&auto=0&height=88\"></iframe>");
    }
    // 支持gist代码输出
    if (TaleConst.BCONF.getBoolean("app.support_gist", true) && content.contains("https://gist.github.com/")) {
        content = content.replaceAll("&lt;script src=\"https://gist.github.com/(\\w+)/(\\w+)\\.js\">&lt;/script>", "<script src=\"https://gist.github.com/$1/$2\\.js\"></script>");
    }

    return content;
}
 
Example #5
Source File: StrikethroughPluginTest.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Test
public void plugin_parser_extension_registered() {
    // configure parser is called with proper parser extension

    final StrikethroughPlugin plugin = StrikethroughPlugin.create();
    final Parser.Builder parserBuilder = mock(Parser.Builder.class);
    plugin.configureParser(parserBuilder);

    //noinspection unchecked
    final ArgumentCaptor<Iterable<Extension>> captor = ArgumentCaptor.forClass(Iterable.class);

    //noinspection unchecked
    verify(parserBuilder, times(1)).extensions(captor.capture());

    final List<Extension> list = Ix.from(captor.getValue()).toList();
    assertEquals(1, list.size());

    assertTrue(list.get(0) instanceof StrikethroughExtension);
}
 
Example #6
Source File: MarkDownProcessor.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private String processCommonMark(String source) {
  Set<Extension> extensions = Collections.singleton(TablesExtension.create());
  Parser parser = Parser.builder().extensions(extensions).build();
  Node document = parser.parse(source);
  HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();
  String html = renderer.render(document);
  html = html.replace("<table>", "<table class=\"grid\">");
  return html;  
}
 
Example #7
Source File: DocumentationPod.java    From symja_android_library with GNU General Public License v3.0 5 votes vote down vote up
private static String generateHTMLString(final String markdownStr) {
	Set<Extension> EXTENSIONS = Collections.singleton(TablesExtension.create());
	Parser parser = Parser.builder().extensions(EXTENSIONS).build();
	Node document = parser.parse(markdownStr);
	HtmlRenderer renderer = HtmlRenderer.builder().extensions(EXTENSIONS).build();
	return renderer.render(document);
}
 
Example #8
Source File: RenderMarkdown.java    From symja_android_library with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {
	Set<Extension> EXTENSIONS = Collections.singleton(TablesExtension.create());
	Parser parser = Parser.builder().extensions(EXTENSIONS).build();
	Node document = parser.parse(TABLE);
	HtmlRenderer renderer = HtmlRenderer.builder().extensions(EXTENSIONS).build();
	String html = renderer.render(document);
	System.out.println(html);
}
 
Example #9
Source File: MarkdownUtil.java    From pybbs with GNU Affero General Public License v3.0 5 votes vote down vote up
public static String render(String content) {
    List<Extension> extensions = Arrays.asList(AutolinkExtension.create(), TablesExtension.create());

    Parser parser = Parser.builder().extensions(extensions).build();
    // 回车一次就可以实现换行
    HtmlRenderer renderer = HtmlRenderer.builder().softbreak("<br/>").attributeProviderFactory(context -> new
            MyAttributeProvider()).extensions(extensions).build();
    Node document = parser.parse(content == null ? "" : content);
    return renderer.render(document);
}
 
Example #10
Source File: AndroidSupportTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void parseWithExtensionsTest(Extension extension) throws Exception {
    Parser parser = Parser.builder()
            .extensions(Collections.singletonList(extension))
            .build();

    Node document = parser.parse(spec);
    assertNotNull(document);

    HtmlRenderer renderer = HtmlRenderer.builder()
            .extensions(Collections.singletonList(extension))
            .build();

    String renderedString = renderer.render(document);
    assertNotNull(renderedString);
}
 
Example #11
Source File: HeadingAnchorConfigurationTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private HtmlRenderer buildRenderer(String defaultId, String prefix, String suffix) {
    Extension ext = HeadingAnchorExtension.builder()
            .defaultId(defaultId)
            .idPrefix(prefix)
            .idSuffix(suffix)
            .build();
    return HtmlRenderer.builder()
            .extensions(Arrays.asList(ext))
            .build();
}
 
Example #12
Source File: MyUtils.java    From Jantent with MIT License 5 votes vote down vote up
/**
 * markdown转换为html
 *
 * @param markdown
 * @return
 */
public static String mdToHtml(String markdown) {
    if (StringUtils.isBlank(markdown)) {
        return "";
    }
    List<Extension> extensions = Arrays.asList(TablesExtension.create());
    Parser          parser     = Parser.builder().extensions(extensions).build();
    Node document = parser.parse(markdown);
    HtmlRenderer renderer = HtmlRenderer.builder()
            .attributeProviderFactory(context -> new LinkAttributeProvider())
            .extensions(extensions).build();
    String content = renderer.render(document);
    content = Commons.emoji(content);
    return content;
}
 
Example #13
Source File: TaleUtils.java    From my-site with Apache License 2.0 5 votes vote down vote up
/**
 * markdown转换为html
 *
 * @param markdown
 * @return
 */
public static String mdToHtml(String markdown) {
    if (StringUtils.isBlank(markdown)) {
        return "";
    }
    java.util.List<Extension> extensions = Arrays.asList(TablesExtension.create());
    Parser parser = Parser.builder().extensions(extensions).build();
    Node document = parser.parse(markdown);
    HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();
    String content = renderer.render(document);
    content = Commons.emoji(content);
    return content;
}
 
Example #14
Source File: MarkDownUtils.java    From My-Blog-layui with Apache License 2.0 5 votes vote down vote up
/**
 * 转换md格式为html
 *
 * @param markdownString
 * @return
 */
public static String mdToHtml(String markdownString) {
    if (StringUtils.isEmpty(markdownString)) {
        return "";
    }
    List<Extension> extensions = Arrays.asList(TablesExtension.create());
    Parser parser = Parser.builder().extensions(extensions).build();
    Node document = parser.parse(markdownString);
    HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();
    return renderer.render(document);
}
 
Example #15
Source File: MarkDownUtil.java    From My-Blog with Apache License 2.0 5 votes vote down vote up
/**
 * 转换md格式为html
 *
 * @param markdownString
 * @return
 */
public static String mdToHtml(String markdownString) {
    if (StringUtils.isEmpty(markdownString)) {
        return "";
    }
    java.util.List<Extension> extensions = Arrays.asList(TablesExtension.create());
    Parser parser = Parser.builder().extensions(extensions).build();
    Node document = parser.parse(markdownString);
    HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();
    String content = renderer.render(document);
    return content;
}
 
Example #16
Source File: StrikethroughExtension.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static Extension create() {
    return new StrikethroughExtension();
}
 
Example #17
Source File: InsExtension.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static Extension create() {
    return new InsExtension();
}
 
Example #18
Source File: YamlFrontMatterExtension.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static Extension create() {
    return new YamlFrontMatterExtension();
}
 
Example #19
Source File: AutolinkExtension.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static Extension create() {
    return new AutolinkExtension();
}
 
Example #20
Source File: ImageAttributesExtension.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static Extension create() {
    return new ImageAttributesExtension();
}
 
Example #21
Source File: HeadingAnchorExtension.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * @return the extension built with default settings
 */
public static Extension create() {
    return new HeadingAnchorExtension(builder());
}
 
Example #22
Source File: HeadingAnchorExtension.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * @return a configured extension
 */
public Extension build() {
    return new HeadingAnchorExtension(this);
}
 
Example #23
Source File: MentionExtension.java    From mvvm-template with GNU General Public License v3.0 4 votes vote down vote up
public static Extension create() {
    return new MentionExtension();
}
 
Example #24
Source File: TaskListItemsExtension.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static Extension create() {
    return new TaskListItemsExtension();
}
 
Example #25
Source File: EmojiExtension.java    From mvvm-template with GNU General Public License v3.0 4 votes vote down vote up
public static Extension create() {
    return new EmojiExtension();
}
 
Example #26
Source File: TablesExtension.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static Extension create() {
    return new TablesExtension();
}
 
Example #27
Source File: MarkdownToHTML.java    From symja_android_library with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Generate markdown links for Symja function reference.
 * 
 * @param sourceLocation
 *            source directory for funtions (*.md) files
 */
public static void generateHTMLString(final File sourceLocation, String function, boolean javadoc) {
	if (sourceLocation.exists()) {
		// Get the list of the files contained in the package
		final String[] files = sourceLocation.list();
		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				if (files[i].endsWith(".md")) {

					String className = files[i].substring(0, files[i].length() - 3);
					if (className.equals(function)) {
						File file = new File(sourceLocation + "/" + files[i]);
						String html;
						try {
							Set<Extension> EXTENSIONS = Collections.singleton(TablesExtension.create());
							Parser parser = Parser.builder().extensions(EXTENSIONS).build();
							Node document = parser.parse(Files.asCharSource(file, Charsets.UTF_8).read());
							HtmlRenderer renderer = HtmlRenderer.builder().extensions(EXTENSIONS).build();
							html = renderer.render(document);
							if (javadoc) {
								String[] lines = html.split("\\n");
								System.out.println("/**");
								for (int j = 0; j < lines.length; j++) {
									if (!lines[j].startsWith("<h2>")) {
										System.out.println(" * " + lines[j]);
									}
								}
								System.out.println(" */");
							} else {
								System.out.println(html);
							}
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}
			}
		}
	}

}