Skip to content

Commit

Permalink
fix:行号控制
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzhihang committed Oct 26, 2022
1 parent 5a06fb5 commit 26a39c9
Showing 1 changed file with 19 additions and 47 deletions.
66 changes: 19 additions & 47 deletions src/main/java/run/halo/prismjs/PrismJSHeadProcessor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package run.halo.prismjs;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IModel;
Expand Down Expand Up @@ -37,8 +38,7 @@ public Mono<Void> process(ITemplateContext context, IModel model, IElementModelS

private String highlightJsScript(String css, String customCss, Boolean lineNumber) {
// language=html
return """
<!-- PluginPrismJS start -->
String script = """
<link rel="stylesheet" href="/plugins/PluginPrismJS/assets/static/themes/%s"/>
<!-- 工具栏 css -->
<link rel="stylesheet" href="/plugins/PluginPrismJS/assets/static/plugins/toolbar/prism-toolbar.min.css"/>
Expand All @@ -53,52 +53,24 @@ private String highlightJsScript(String css, String customCss, Boolean lineNumbe
<!-- 复制到剪贴板 -->
<script src="/plugins/PluginPrismJS/assets/static/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", async function () {
var customCss = %s;
if (customCss !== null && customCss !== undefined && customCss !== '') {
await loadCss(`customCss`);
console.log("CustomCss: ", customCss);
}
if (%s) {
console.log("ShowLineNumber");
await loadCss(`/plugins/PluginPrismJS/assets/static/plugins/line-numbers/prism-line-numbers.min.css`);
await loadScript(`/plugins/PluginPrismJS/assets/static/plugins/line-numbers/prism-line-numbers.min.js`);
}
})
function loadScript(url) {
return new Promise(function (resolve, reject) {
const script = document.createElement("script");
script.type = "text/javascript";
script.src = url;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
function loadCss(url) {
return new Promise(function (resolve, reject) {
const css = document.createElement("link");
css.rel = "stylesheet";
css.href = url;
css.onload = resolve;
css.onerror = reject;
document.head.appendChild(css);
});
}
</script>
""".formatted(css);

if (lineNumber) {
script = script + """
<!-- 行号 css -->
<link rel="stylesheet" href="/plugins/PluginPrismJS/assets/static/plugins/line-numbers/prism-line-numbers.min.css"/>
<!-- PluginPrismJS end -->
""".formatted(css, customCss, lineNumber);
<script src="/plugins/PluginPrismJS/assets/static/plugins/line-numbers/prism-line-numbers.min.js"></script>
""";
}

if (StringUtils.isNotBlank(customCss)) {
script = script + """
<link rel="stylesheet" href="%s"/>
""".formatted(customCss);
}

return script;
}

@Data
Expand Down

0 comments on commit 26a39c9

Please sign in to comment.