Seperated Markdown into two macros: Markdown and Markdown From URL

This commit is contained in:
unknown
2018-08-08 18:14:00 -05:00
parent 8a4b73b7bd
commit 14e81297dd
5 changed files with 185 additions and 32 deletions

View File

@@ -15,7 +15,7 @@
<url>http://www.atlassian.com/</url> <url>http://www.atlassian.com/</url>
</organization> </organization>
<name>confluence-markdown-macro</name> <name>confluence-markdown-macro</name>
<description>This plugin provides a Markdown render macro for Confluence.</description> <description>This plugin provides macros to render markdown for Confluence.</description>
<packaging>atlassian-plugin</packaging> <packaging>atlassian-plugin</packaging>
<dependencies> <dependencies>
<dependency> <dependency>

View File

@@ -0,0 +1,154 @@
package com.atlassian.plugins.confluence.markdown;
import com.atlassian.confluence.content.render.xhtml.ConversionContext;
import com.atlassian.confluence.content.render.xhtml.DefaultConversionContext;
import com.atlassian.confluence.content.render.xhtml.XhtmlException;
import com.atlassian.confluence.macro.Macro;
import com.atlassian.confluence.macro.MacroExecutionException;
import com.atlassian.confluence.xhtml.api.MacroDefinition;
import com.atlassian.confluence.xhtml.api.MacroDefinitionHandler;
import com.atlassian.confluence.xhtml.api.XhtmlContent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.atlassian.renderer.RenderContext;
import com.atlassian.renderer.v2.RenderMode;
import com.atlassian.renderer.v2.macro.BaseMacro;
import com.atlassian.renderer.v2.macro.MacroException;
//import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.webresource.api.assembler.PageBuilderService;
import org.springframework.beans.factory.annotation.Autowired;
import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughSubscriptExtension;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.ext.ins.InsExtension;
import com.vladsch.flexmark.ext.definition.DefinitionExtension;
import com.vladsch.flexmark.ext.gfm.tasklist.TaskListExtension;
import com.vladsch.flexmark.ext.footnotes.FootnoteExtension;
import com.vladsch.flexmark.ext.wikilink.WikiLinkExtension;
import com.vladsch.flexmark.ext.autolink.AutolinkExtension;
import com.vladsch.flexmark.ext.anchorlink.AnchorLinkExtension;
import com.vladsch.flexmark.superscript.SuperscriptExtension;
import com.vladsch.flexmark.ext.youtube.embedded.YouTubeLinkExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.options.MutableDataSet;
import java.net.*;
import java.io.*;
//@Scanned
public class MarkdownFromURLMacro extends BaseMacro implements Macro
{
private final XhtmlContent xhtmlUtils;
private PageBuilderService pageBuilderService;
@Autowired
public MarkdownFromURLMacro(@ComponentImport PageBuilderService pageBuilderService, XhtmlContent xhtmlUtils) {
this.pageBuilderService = pageBuilderService;
this.xhtmlUtils = xhtmlUtils;
}
// public MarkdownFromURLMacro(XhtmlContent xhtmlUtils)
// {
// this.xhtmlUtils = xhtmlUtils;
// }
@Override
public BodyType getBodyType()
{
return BodyType.PLAIN_TEXT;
}
@Override
public OutputType getOutputType()
{
return OutputType.BLOCK;
}
@Override
public String execute(Map<String, String> parameters, String bodyContent, ConversionContext conversionContext) throws MacroExecutionException
{
pageBuilderService.assembler().resources().requireWebResource("com.atlassian.plugins.confluence.markdown.confluence-markdown-macro:highlightjs");
MutableDataSet options = new MutableDataSet();
options.set(Parser.EXTENSIONS, Arrays.asList(
TablesExtension.create(),
StrikethroughSubscriptExtension.create(),
InsExtension.create(),
TaskListExtension.create(),
FootnoteExtension.create(),
WikiLinkExtension.create(),
DefinitionExtension.create(),
AnchorLinkExtension.create(),
AutolinkExtension.create(),
SuperscriptExtension.create(),
YouTubeLinkExtension.create()
));
String highlightjs = "<script>\n" +
"AJS.$('[data-macro-name=\"markdown\"] code').each(function(i, block) {\n" +
" hljs.highlightBlock(block);\n" +
" });\n" +
"</script>";
if (bodyContent != null) {
Parser parser = Parser.builder(options).build();
HtmlRenderer renderer = HtmlRenderer.builder(options).build();
String toParse = "";
try {
URL importFrom = new URL(bodyContent);
BufferedReader in = new BufferedReader(
new InputStreamReader(importFrom.openStream())
);
String inputLine;
toParse = "";
while ((inputLine = in.readLine()) != null) {
toParse = toParse + "\n" + inputLine;
}
in.close();
}
catch (IOException e) {}
Node document = parser.parse(toParse);
String html = renderer.render(document) + highlightjs;
return html;
}else {
return "";
}
}
@Override
public boolean hasBody() {
return true; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public RenderMode getBodyRenderMode() {
return RenderMode.NO_RENDER; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String execute(Map map, String s, RenderContext renderContext) throws MacroException {
try {
return execute(map, s, new DefaultConversionContext(renderContext));
} catch (MacroExecutionException e) {
throw new MacroException(e.getMessage(),e);
}
}
}

View File

@@ -40,8 +40,6 @@ import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser; import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.options.MutableDataSet; import com.vladsch.flexmark.util.options.MutableDataSet;
import java.net.*;
import java.io.*;
//@Scanned //@Scanned
public class MarkdownMacro extends BaseMacro implements Macro public class MarkdownMacro extends BaseMacro implements Macro
@@ -105,31 +103,12 @@ public class MarkdownMacro extends BaseMacro implements Macro
" });\n" + " });\n" +
"</script>"; "</script>";
if (bodyContent != null) { Parser parser = Parser.builder(options).build();
Parser parser = Parser.builder(options).build(); HtmlRenderer renderer = HtmlRenderer.builder(options).build();
HtmlRenderer renderer = HtmlRenderer.builder(options).build();
Node document = parser.parse(bodyContent);
String toParse = ""; String html = renderer.render(document ) + highlightjs; // "<p>This is <em>Sparta</em></p>\n"
try { return html;
URL importFrom = new URL(bodyContent);
BufferedReader in = new BufferedReader(
new InputStreamReader(importFrom.openStream())
);
String inputLine;
toParse = "";
while ((inputLine = in.readLine()) != null) {
toParse = toParse + "\n" + inputLine;
}
in.close();
}
catch (IOException e) {}
Node document = parser.parse(toParse);
String html = renderer.render(document) + highlightjs;
return html;
}else {
return "";
}
} }

View File

@@ -7,24 +7,41 @@
<param name="plugin-logo">images/pluginLogo.png</param> <param name="plugin-logo">images/pluginLogo.png</param>
<param name="atlassian-data-center-compatible">true</param> <param name="atlassian-data-center-compatible">true</param>
</plugin-info> </plugin-info>
<xhtml-macro name="markdown" <xhtml-macro name="markdown"
class="com.atlassian.plugins.confluence.markdown.MarkdownMacro" class="com.atlassian.plugins.confluence.markdown.MarkdownMacro"
key="markdown" key="markdown"
icon="/download/resources/com.atlassian.plugins.confluence.markdown.confluence-markdown-macro/images/pluginIcon.png" icon="/download/resources/com.atlassian.plugins.confluence.markdown.confluence-markdown-macro/images/pluginIcon.png"
documentation-url="https://spec.commonmark.org/0.28/"> documentation-url="https://spec.commonmark.org/0.28/">
<category name="formatting"/> <category name="formatting"/>
<parameters /> <parameters />
</xhtml-macro>
<macro name="markdown" <macro name="markdown"
class="com.atlassian.plugins.confluence.markdown.MarkdownMacro" class="com.atlassian.plugins.confluence.markdown.MarkdownMacro"
key="markdown.wiki" key="markdown.wiki"
icon="/download/resources/com.atlassian.plugins.confluence.markdown.confluence-markdown-macro/images/pluginIcon.png" icon="/download/resources/com.atlassian.plugins.confluence.markdown.confluence-markdown-macro/images/pluginIcon.png"
documentation-url="https://spec.commonmark.org/0.28/"> documentation-url="https://spec.commonmark.org/0.28/">
<category name="formatting"/> <category name="formatting"/>
<parameters> <parameters />
</parameters> </macro>
<xhtml-macro name="markdown-from-url"
class="com.atlassian.plugins.confluence.markdown.MarkdownFromURLMacro"
key="markdown-from-url"
icon="/download/resources/com.atlassian.plugins.confluence.markdown.confluence-markdown-macro/images/pluginIcon.png"
documentation-url="https://spec.commonmark.org/0.28/">
<category name="formatting"/>
<parameters />
</xhtml-macro>
<macro name="markdown-from-url"
class="com.atlassian.plugins.confluence.markdown.MarkdownFromURLMacro"
key="markdown-from-url.wiki"
icon="/download/resources/com.atlassian.plugins.confluence.markdown.confluence-markdown-macro/images/pluginIcon.png"
documentation-url="https://spec.commonmark.org/0.28/">
<category name="formatting"/>
<parameters />
</macro> </macro>
<resource type="i18n" name="markdown" location="markdownproperties/markdown"/> <resource type="i18n" name="markdown" location="markdownproperties/markdown"/>
<resource type="i18n" name="markdown-from-url" location="markdown-from-url-properties/markdown-from-url"/>
<resource type="download" name="images/" key="images" location="images/"/> <resource type="download" name="images/" key="images" location="images/"/>
<web-resource key="highlightjs" name="Highlight.js" > <web-resource key="highlightjs" name="Highlight.js" >

View File

@@ -0,0 +1,3 @@
com.atlassian.plugins.confluence.markdown.confluence-markdown-macro.markdown-from-url.label=Markdown From URL
com.atlassian.plugins.confluence.markdown.confluence-markdown-macro.markdown-from-url.desc=This macro imports Markdown from a URL and renders it into HTML.
com.atlassian.plugins.confluence.markdown.confluence-markdown-macro.markdown-from-url.body.desc=Type URL here