org.commonmark.node.Heading Java Examples

The following examples show how to use org.commonmark.node.Heading. 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: CorePlugin.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: TableOfContentsPlugin.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeRender(@NonNull Node node) {

    // custom block to hold TOC
    final TableOfContentsBlock block = new TableOfContentsBlock();

    // create TOC title
    {
        final Text text = new Text("Table of contents");
        final Heading heading = new Heading();
        // important one - set TOC heading level
        heading.setLevel(1);
        heading.appendChild(text);
        block.appendChild(heading);
    }

    final HeadingVisitor visitor = new HeadingVisitor(block);
    node.accept(visitor);

    // make it the very first node in rendered markdown
    node.prependChild(block);
}
 
Example #3
Source File: CommonmarkPreviewRenderer.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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: RuleService.java    From clouditor with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(Heading heading) {
  var title = renderText(heading).trim();

  switch (heading.getLevel()) {
    case 1:
      // update the name
      this.rule.setName(title);

      // update the description
      var next = heading.getNext();

      // if the next one is a Heading, then we have no description
      if (!(next instanceof Heading)) {
        this.rule.setDescription(renderHTML(next));
      }

      // additionally, visit all children of the heading to parse code blocks
      this.visitChildren(heading);

      break;
    case 2:
      if (title.equals("Controls")) {
        var node = heading.getNext();

        node.accept(new ControlsVisitor(this.rule));
      }
      break;
    default:
      super.visit(heading);
  }
}
 
Example #5
Source File: CorePlugin.java    From Markwon with Apache License 2.0 5 votes vote down vote up
/**
 * @return a set with enabled by default block types
 * @since 4.4.0
 */
@NonNull
public static Set<Class<? extends Block>> enabledBlockTypes() {
    return new HashSet<>(Arrays.asList(
            BlockQuote.class,
            Heading.class,
            FencedCodeBlock.class,
            HtmlBlock.class,
            ThematicBreak.class,
            ListBlock.class,
            IndentedCodeBlock.class
    ));
}
 
Example #6
Source File: HeadingHandler.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Object getSpans(
        @NonNull MarkwonConfiguration configuration,
        @NonNull RenderProps renderProps,
        @NonNull HtmlTag tag) {

    final SpanFactory factory = configuration.spansFactory().get(Heading.class);
    if (factory == null) {
        return null;
    }

    int level;
    try {
        level = Integer.parseInt(tag.name().substring(1));
    } catch (NumberFormatException e) {
        e.printStackTrace();
        level = 0;
    }

    if (level < 1 || level > 6) {
        return null;
    }

    CoreProps.HEADING_LEVEL.set(renderProps, level);

    return factory.getSpans(configuration, renderProps);
}
 
Example #7
Source File: TableOfContentsPlugin.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@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 #8
Source File: DocMaker.java    From jeka with Apache License 2.0 5 votes vote down vote up
private List<MenuItem> addAnchorAndNumberingToHeaders(Node node) {
    final List<MenuItem> menuItems = new LinkedList<>();
    final int[] counters = new int[10];
    node.accept(new AbstractVisitor() {

        @Override
        public void visit(Heading heading) {
            final Text text = (Text) heading.getFirstChild();
            final String content = text.getLiteral();
            final boolean intro = "Introduction".equals(content);  // Do not number Introduction
            if (!intro) {
                counters[heading.getLevel()]++;
                for (int i = heading.getLevel() + 1; i < 6; i++) {
                    counters[i] = 0;
                }
            }
            final StringBuilder sb = new StringBuilder();
            for (int i = 1; i <= heading.getLevel(); i++) {
                sb.append(counters[i]).append(".");
            }
            if (sb.length() > 1 && heading.getLevel() > 1) {
                sb.delete(sb.length() - 1, sb.length() );
            }
            final String anchorId = content.replace(" ", "");
            final HtmlInline htmlInline = new HtmlInline();
            htmlInline.setLiteral("<a name=\"" + anchorId + "\"></a>");
            heading.insertBefore(htmlInline);
            final MenuItem menuItem = new MenuItem(content, anchorId, heading.getLevel());
            menuItems.add(menuItem);
        }
    });
    return menuItems;
}
 
Example #9
Source File: MarkwonVisitorImpl.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(Heading heading) {
    visit((Node) heading);
}