001/*
002 * Shredzone Commons
003 *
004 * Copyright (C) 2021 Richard "Shred" Körber
005 *   http://commons.shredzone.org
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU Library General Public License as
009 * published by the Free Software Foundation, either version 3 of the
010 * License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU Library General Public License
018 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
019 */
020package org.shredzone.commons.text.filter;
021
022import java.util.List;
023
024import org.commonmark.Extension;
025import org.commonmark.ext.autolink.AutolinkExtension;
026import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
027import org.commonmark.ext.gfm.tables.TablesExtension;
028import org.commonmark.ext.heading.anchor.HeadingAnchorExtension;
029import org.commonmark.ext.task.list.items.TaskListItemsExtension;
030
031/**
032 * A filter that converts Markdown to HTML, but adding GitHub Flavored Markdown.
033 *
034 * @see <a href="https://github.github.com/gfm/">GitHub Flavored Markdown Spec</a>
035 * @see <a href="https://github.com/atlassian/commonmark-java">commonmark-java</a>
036 * @author Richard "Shred" Körber
037 * @since 2.8
038 */
039public class GithubFlavoredMarkdownFilter extends MarkdownFilter {
040    private boolean autolink;
041
042    /**
043     * Enables the autolink extension. It is disabled by default, to give the user full
044     * control about link placement in the text.
045     *
046     * @param autolink
047     *         {@code true} to enable the autolink extension. {@code false} by default.
048     */
049    public void setAutolinkEnabled(boolean autolink) {
050        this.autolink = autolink;
051    }
052
053    @Override
054    protected List<Extension> createExtensionList() {
055        List<Extension> extensions = super.createExtensionList();
056        extensions.add(TablesExtension.create());
057        extensions.add(TaskListItemsExtension.create());
058        extensions.add(StrikethroughExtension.create());
059        extensions.add(HeadingAnchorExtension.create());
060        if (autolink) {
061            extensions.add(AutolinkExtension.create());
062        }
063        return extensions;
064    }
065
066}