org.commonmark.node.Link Java Examples
The following examples show how to use
org.commonmark.node.Link.
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: DocMaker.java From jeka with Apache License 2.0 | 6 votes |
private void addMenu(Node document, List<MenuItem> menuItems) { final List<MenuItem> reversedItems = new LinkedList<>(menuItems); Collections.reverse(reversedItems); for (final MenuItem menuItem : reversedItems) { if (menuItem.level > 5) { continue; } final Link link = new Link(); link.setTitle(menuItem.title); final Text text = new Text(); text.setLiteral( menuItem.title); link.appendChild(text); link.setDestination("#" + menuItem.anchorId); final HtmlInline indent = new HtmlInline(); final String cssClass = "menuItem" + menuItem.level; indent.setLiteral("<a href=\"#" + menuItem.anchorId + "\" class=\"" + cssClass + "\">" + menuItem.title + "</a>"); document.prependChild(indent); document.prependChild(new HardLineBreak()); } }
Example #2
Source File: CorePlugin.java From Markwon with Apache License 2.0 | 6 votes |
@Override public void configureSpansFactory(@NonNull MarkwonSpansFactory.Builder builder) { // reuse this one for both code-blocks (indent & fenced) final CodeBlockSpanFactory codeBlockSpanFactory = new CodeBlockSpanFactory(); builder .setFactory(StrongEmphasis.class, new StrongEmphasisSpanFactory()) .setFactory(Emphasis.class, new EmphasisSpanFactory()) .setFactory(BlockQuote.class, new BlockQuoteSpanFactory()) .setFactory(Code.class, new CodeSpanFactory()) .setFactory(FencedCodeBlock.class, codeBlockSpanFactory) .setFactory(IndentedCodeBlock.class, codeBlockSpanFactory) .setFactory(ListItem.class, new ListItemSpanFactory()) .setFactory(Heading.class, new HeadingSpanFactory()) .setFactory(Link.class, new LinkSpanFactory()) .setFactory(ThematicBreak.class, new ThematicBreakSpanFactory()); }
Example #3
Source File: CommonmarkPreviewRenderer.java From markdown-writer-fx with BSD 2-Clause "Simplified" License | 6 votes |
private void printAttributes(StringBuilder buf, Node node) { if (node instanceof Text) printAttribute(buf, "literal", ((Text)node).getLiteral()); else if (node instanceof Code) printAttribute(buf, "literal", ((Code)node).getLiteral()); else if (node instanceof IndentedCodeBlock) printAttribute(buf, "literal", ((IndentedCodeBlock)node).getLiteral()); else if (node instanceof FencedCodeBlock) printAttribute(buf, "literal", ((FencedCodeBlock)node).getLiteral()); else if (node instanceof HtmlBlock) printAttribute(buf, "literal", ((HtmlBlock)node).getLiteral()); else if (node instanceof HtmlInline) printAttribute(buf, "literal", ((HtmlInline)node).getLiteral()); else if (node instanceof Link) { printAttribute(buf, "destination", ((Link)node).getDestination()); printAttribute(buf, "title", ((Link)node).getTitle()); } else if (node instanceof Image) { printAttribute(buf, "destination", ((Image)node).getDestination()); printAttribute(buf, "title", ((Image)node).getTitle()); } else if (node instanceof Heading) printAttribute(buf, "level", ((Heading)node).getLevel()); }
Example #4
Source File: AutolinkPostProcessor.java From commonmark-java with BSD 2-Clause "Simplified" License | 6 votes |
private void linkify(Text textNode) { String literal = textNode.getLiteral(); Node lastNode = textNode; for (Span span : linkExtractor.extractSpans(literal)) { String text = literal.substring(span.getBeginIndex(), span.getEndIndex()); if (span instanceof LinkSpan) { String destination = getDestination((LinkSpan) span, text); Text contentNode = new Text(text); Link linkNode = new Link(destination, null); linkNode.appendChild(contentNode); lastNode = insertNode(linkNode, lastNode); } else { lastNode = insertNode(new Text(text), lastNode); } } // Original node no longer needed textNode.unlink(); }
Example #5
Source File: LinkHandler.java From Markwon with Apache License 2.0 | 6 votes |
@Nullable @Override public Object getSpans(@NonNull MarkwonConfiguration configuration, @NonNull RenderProps renderProps, @NonNull HtmlTag tag) { final String destination = tag.attributes().get("href"); if (!TextUtils.isEmpty(destination)) { final SpanFactory spanFactory = configuration.spansFactory().get(Link.class); if (spanFactory != null) { CoreProps.LINK_DESTINATION.set( renderProps, destination ); return spanFactory.getSpans(configuration, renderProps); } } return null; }
Example #6
Source File: CustomExtensionActivity2.java From Markwon with Apache License 2.0 | 6 votes |
private void setLink(@NonNull MarkwonVisitor visitor, @NonNull String destination, int start, int end) { // might a simpler one, but it doesn't respect possible changes to links // visitor.builder().setSpan( // new LinkSpan(visitor.configuration().theme(), destination, visitor.configuration().linkResolver()), // start, // end // ); // use default handlers for links final MarkwonConfiguration configuration = visitor.configuration(); final RenderProps renderProps = visitor.renderProps(); CoreProps.LINK_DESTINATION.set(renderProps, destination); SpannableBuilder.setSpans( visitor.builder(), configuration.spansFactory().require(Link.class).getSpans(configuration, renderProps), start, end ); }
Example #7
Source File: CustomExtensionActivity2.java From Markwon with Apache License 2.0 | 5 votes |
@Override protected Node parse() { final String user = match(RE); if (user != null) { final Link link = new Link(createUserLinkDestination(user), null); link.appendChild(text("@" + user)); return link; } return null; }
Example #8
Source File: MustacheCustomTag.java From alf.io with GNU General Public License v3.0 | 5 votes |
@Override public void setAttributes(Node node, String tagName, Map<String, String> attributes) { if (node instanceof Link) { Link l = (Link) node; String destination = StringUtils.trimToEmpty(l.getDestination()); if (UrlUtils.isAbsoluteUrl(destination)) { attributes.put("target", "_blank"); attributes.put("rel", "nofollow noopener noreferrer"); } } }
Example #9
Source File: LinkifyPlugin.java From Markwon with Apache License 2.0 | 5 votes |
@Override public void onTextAdded(@NonNull MarkwonVisitor visitor, @NonNull String text, int start) { // @since 4.2.0 obtain span factory for links // we will be using the link that is used by markdown (instead of directly applying URLSpan) final SpanFactory spanFactory = visitor.configuration().spansFactory().get(Link.class); if (spanFactory == null) { return; } // @since 4.2.0 we no longer re-use builder (thread safety achieved for // render calls from different threads and ... better performance) final SpannableStringBuilder builder = new SpannableStringBuilder(text); if (addLinks(builder, mask)) { // target URL span specifically final URLSpan[] spans = builder.getSpans(0, builder.length(), URLSpan.class); if (spans != null && spans.length > 0) { final RenderProps renderProps = visitor.renderProps(); final SpannableBuilder spannableBuilder = visitor.builder(); for (URLSpan span : spans) { CoreProps.LINK_DESTINATION.set(renderProps, span.getURL()); SpannableBuilder.setSpans( spannableBuilder, spanFactory.getSpans(visitor.configuration(), renderProps), start + builder.getSpanStart(span), start + builder.getSpanEnd(span) ); } } } }
Example #10
Source File: TableOfContentsPlugin.java From Markwon with Apache License 2.0 | 5 votes |
@Override public void visit(Heading heading) { this.isInsideHeading = true; try { // reset build from previous content builder.setLength(0); // obtain level (can additionally filter by level, to skip lower ones) final int level = heading.getLevel(); // build heading title visitChildren(heading); // initial list item final ListItem listItem = new ListItem(); Node parent = listItem; Node node = listItem; for (int i = 1; i < level; i++) { final ListItem li = new ListItem(); final BulletList bulletList = new BulletList(); bulletList.appendChild(li); parent.appendChild(bulletList); parent = li; node = li; } final String content = builder.toString(); final Link link = new Link("#" + AnchorHeadingPlugin.createAnchor(content), null); final Text text = new Text(content); link.appendChild(text); node.appendChild(link); bulletList.appendChild(listItem); } finally { isInsideHeading = false; } }
Example #11
Source File: CustomExtensionActivity2.java From Markwon with Apache License 2.0 | 5 votes |
@Override protected Node parse() { final String id = match(RE); if (id != null) { final Link link = new Link(createIssueOrPullRequestLinkDestination(id), null); link.appendChild(text("#" + id)); return link; } return null; }
Example #12
Source File: MarkwonSpansFactoryImplTest.java From Markwon with Apache License 2.0 | 5 votes |
@Test public void builder() { // all passed to builder will be in factory final SpanFactory text = mock(SpanFactory.class); final SpanFactory link = mock(SpanFactory.class); final MarkwonSpansFactory factory = new MarkwonSpansFactoryImpl.BuilderImpl() .setFactory(Text.class, text) .setFactory(Link.class, link) .build(); assertNotNull(factory.get(Text.class)); assertNotNull(factory.get(Link.class)); // a bunch of non-present factories //noinspection unchecked final Class<? extends Node>[] types = new Class[]{ Image.class, Block.class, Emphasis.class, Paragraph.class }; for (Class<? extends Node> type : types) { assertNull(factory.get(type)); } }
Example #13
Source File: MarkwonSpansFactoryTest.java From Markwon with Apache License 2.0 | 5 votes |
@Test public void builder_require_fail() { try { builder.requireFactory(Link.class); fail(); } catch (NullPointerException e) { assertTrue(e.getMessage(), e.getMessage().contains(Link.class.getName())); } }
Example #14
Source File: PageTextRenderer.java From jweb-cms with GNU Affero General Public License v3.0 | 5 votes |
@Override public void render(Node node) { Text text = (Text) node; if (text.getParent() != null && text.getParent() instanceof Link) { html.text(text.getLiteral()); } else { CreateInnerLinksResult result = keywordService.createInnerLinks(text.getLiteral()); if (result.inserted) { html.raw(result.result); } else { html.text(text.getLiteral()); } } }
Example #15
Source File: MyUtils.java From Jantent with MIT License | 4 votes |
@Override public void setAttributes(Node node, String tagName, Map<String, String> attributes) { if (node instanceof Link) { attributes.put("target", "_blank"); } }
Example #16
Source File: AutolinkPostProcessor.java From commonmark-java with BSD 2-Clause "Simplified" License | 4 votes |
@Override public void visit(Link link) { inLink++; super.visit(link); inLink--; }
Example #17
Source File: MarkwonVisitorImpl.java From Markwon with Apache License 2.0 | 4 votes |
@Override public void visit(Link link) { visit((Node) link); }