Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#103 markedjs update and preview links added #107

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand All @@ -39,18 +38,26 @@
public class MarkedMarkdownRenderer implements MarkdownRenderer {

public final Context context;

public final Value markedFunction;
public final Source markedSource;
public MarkedMarkdownRenderer (final Context context) {

public MarkedMarkdownRenderer(final Context context) {
try {
this.context = context;

var content = new String(MarkedMarkdownRenderer.class.getResourceAsStream("marked.min.js").readAllBytes(), StandardCharsets.UTF_8);
markedSource = Source.newBuilder("js", content, "marked.mjs").build();
context.eval(markedSource);
markedFunction = context.eval("js", "(function (param) {return marked.parse(param);})");

var markedFunctionSource = """
(function (param) {
%s
return marked.parse(param);
})
""".formatted(preview());

markedFunction = context.eval("js", markedFunctionSource);
} catch (IOException ex) {
log.error(null, ex);
throw new RuntimeException(ex);
Expand All @@ -61,14 +68,56 @@ public MarkedMarkdownRenderer (final Context context) {
public void close() {
context.close(true);
}




private String preview() {
return """
var PreviewContext = Java.type('com.github.thmarx.cms.api.PreviewContext');

if (PreviewContext.IS_PREVIEW.get()) {
console.log(PreviewContext.IS_PREVIEW.get())
const cleanUrl = (href) => {
try {
href = encodeURI(href).replace(/%25/g, '%');
} catch (e) {
return null;
}
return href;
}
const renderer = {
link(href, title, text) {
const cleanHref = cleanUrl(href);
if (cleanHref === null) {
return text;
}
href = cleanHref;
// is relativ internal url
if (!href.startsWith("http://") && !href.startsWith("https://")) {
if (href.includes("?")) {
href += "&preview"
} else {
href += "?preview"
}
}

let out = '<a href="' + href + '"';
if (title) {
out += ' title="' + title + '"';
}
out += '>' + text + '</a>';
return out;
}
}
marked.use({ renderer });

}
""";
}

@Override
public String excerpt(String markdown, int length) {
var content = markedFunction.execute(markdown).asString();
String text = Jsoup.parse(content).text();

if (text.length() <= length) {
return text;
} else {
Expand All @@ -80,5 +129,5 @@ public String excerpt(String markdown, int length) {
public String render(String markdown) {
return markedFunction.execute(markdown).asString();
}

}
Loading