Skip to content

Commit

Permalink
🐛 Add version param in render script
Browse files Browse the repository at this point in the history
  • Loading branch information
justice2001 committed Jan 16, 2024
1 parent 4724bc0 commit cb6b6cc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.google.common.base.Throwables;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.pf4j.PluginWrapper;
import org.springframework.stereotype.Component;
import org.springframework.util.PropertyPlaceholderHelper;
import reactor.core.publisher.Mono;
import run.halo.app.plugin.ReactiveSettingFetcher;
import run.halo.app.theme.ReactivePostContentHandler;
Expand All @@ -17,13 +19,16 @@ public class VditorPostContentHandler implements ReactivePostContentHandler {

private final ReactiveSettingFetcher reactiveSettingFetcher;

private final PluginWrapper pluginWrapper;

@Override
public Mono<PostContentContext> handle(PostContentContext contentContext) {
return reactiveSettingFetcher.fetch("render", RenderConfig.class)
.map(renderConfig -> {
if (renderConfig.getEnableRender()&&
(!renderConfig.getOnlyMarkdown() || contentContext.getRawType().equals("markdown"))) {
contentContext.setContent(ScriptUtils.renderScript(renderConfig) + "\n" + contentContext.getContent());
var content = ScriptUtils.renderScript(renderConfig) + "\n" + contentContext.getContent();
contentContext.setContent(ScriptUtils.setContentProperty(content, pluginWrapper));
}
return contentContext;
})
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/top/mczhengyi/vditor/utils/ScriptUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package top.mczhengyi.vditor.utils;

import org.pf4j.PluginWrapper;
import org.springframework.util.PropertyPlaceholderHelper;
import top.mczhengyi.vditor.bean.RenderConfig;
import java.util.Properties;

public class ScriptUtils {
static final PropertyPlaceholderHelper
PROPERTY_PLACEHOLDER_HELPER = new PropertyPlaceholderHelper("${", "}");

public static String renderScript(RenderConfig renderConfig) {
StringBuilder script = new StringBuilder();
script.append(basicScript(renderConfig));
Expand All @@ -14,9 +20,9 @@ public static String renderScript(RenderConfig renderConfig) {

public static String basicScript(RenderConfig renderConfig) {
return """
<link rel="stylesheet" type="text/css" href="/plugins/vditor-mde/assets/static/vditor-render.css" id="vditor-style" />
<script src="/plugins/vditor-mde/assets/static/dist/method.min.js"></script>
<script src="/plugins/vditor-mde/assets/static/render.js" id="vditor-render"
<link rel="stylesheet" type="text/css" href="/plugins/vditor-mde/assets/static/vditor-render.css?version=${version}" id="vditor-style" />
<script src="/plugins/vditor-mde/assets/static/dist/method.min.js?version=${version}"></script>
<script src="/plugins/vditor-mde/assets/static/render.js?version=${version}" id="vditor-render"
data-dark="%s" data-mediaRender="%s"></script>
""".formatted(renderConfig.getDarkMode(), renderConfig.getMediaRender());
}
Expand All @@ -41,4 +47,10 @@ public static String joeDarkMode() {
</script>
""";
}

public static String setContentProperty(String script, PluginWrapper pluginWrapper) {
final Properties properties = new Properties();
properties.setProperty("version", pluginWrapper.getDescriptor().getVersion());
return PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders(script, properties);
}
}

0 comments on commit cb6b6cc

Please sign in to comment.