🐛 Add version param in render script
Build Plugin JAR File / build (push) Waiting to run Details
Build Plugin JAR File / github-release (push) Blocked by required conditions Details

Fix https://github.com/justice2001/halo-plugin-vditor/issues/19
Fix #49
This commit is contained in:
zhengyi 2024-01-16 21:00:22 +08:00
parent 4724bc04e9
commit cb6b6cc224
2 changed files with 21 additions and 4 deletions

View File

@ -3,7 +3,9 @@ package top.mczhengyi.vditor.extension;
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;
@ -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;
})

View File

@ -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));
@ -14,9 +20,9 @@ public class ScriptUtils {
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());
}
@ -41,4 +47,10 @@ public class ScriptUtils {
</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);
}
}