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

feat: add built-in transformers #3

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 34 additions & 8 deletions src/main/java/run/halo/shiki/ShikiHeadProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Properties;

import org.pf4j.PluginWrapper;
import org.springframework.stereotype.Component;
import org.springframework.util.PropertyPlaceholderHelper;
import org.thymeleaf.context.ITemplateContext;
Expand All @@ -13,6 +12,7 @@
import lombok.RequiredArgsConstructor;
import reactor.core.publisher.Mono;
import run.halo.app.plugin.PluginContext;
import run.halo.app.plugin.ReactiveSettingFetcher;
import run.halo.app.theme.dialect.TemplateHeadProcessor;

/**
Expand All @@ -26,23 +26,49 @@ public class ShikiHeadProcessor implements TemplateHeadProcessor {

private final PluginContext pluginContext;

private final ReactiveSettingFetcher settingFetcher;

@Override
public Mono<Void> process(ITemplateContext context, IModel model,
IElementModelStructureHandler structureHandler) {
final IModelFactory modelFactory = context.getModelFactory();
model.add(modelFactory.createText(commentWidgetScript()));
return Mono.empty();
public Mono<Void> process(ITemplateContext context, IModel model, IElementModelStructureHandler structureHandler) {
return settingFetcher.fetch(CustomSetting.GROUP, CustomSetting.class)
.doOnNext(customSetting -> {
final IModelFactory modelFactory = context.getModelFactory();
model.add(
modelFactory.createText(
commentWidgetScript(customSetting.themeLight(), customSetting.themeDark(),
customSetting.useBuiltinStyle())));
})
.then();
}

private String commentWidgetScript() {
private String commentWidgetScript(String themeLight, String themeDark, boolean useBuiltinStyle) {

final Properties properties = new Properties();
properties.setProperty("version", pluginContext.getVersion());
final String version = pluginContext.getVersion();
properties.setProperty("version", version);
properties.setProperty("themeLight", themeLight);
properties.setProperty("themeDark", themeDark);
properties.setProperty("builtinStyle",
useBuiltinStyle
? "<link rel=\"stylesheet\" href=\"/plugins/shiki/assets/static/style.css?version=" + version
+ "\" />"
: "");

return PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders("""
<!-- plugin-shiki start -->
${builtinStyle}
<script src="/plugins/shiki/assets/static/main.js?version=${version}" defer></script>
<script>
window.shikiConfig = {
themeLight: "${themeLight}" === "null" ? "github-light" : "${themeLight}",
themeDark: "${themeDark}" === "null" ? "github-dark" : "${themeDark}",
};
</script>
<!-- plugin-shiki end -->
""", properties);
}

public record CustomSetting(String themeLight, String themeDark, boolean useBuiltinStyle) {
public static final String GROUP = "config";
}
}
2 changes: 1 addition & 1 deletion src/main/java/run/halo/shiki/ShikiPlugin.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package run.halo.starter;
package run.halo.shiki;

import org.springframework.stereotype.Component;
import run.halo.app.plugin.BasePlugin;
Expand Down
130 changes: 130 additions & 0 deletions src/main/resources/extensions/settings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
theme-options: &theme-options
options:
- value: "andromeeda"
label: "Andromeeda"
- value: "aurora-x"
label: "Aurora X"
- value: "ayu-dark"
label: "Ayu Dark"
- value: "catppuccin-frappe"
label: "Catppuccin Frappé"
- value: "catppuccin-latte"
label: "Catppuccin Latte"
- value: "catppuccin-macchiato"
label: "Catppuccin Macchiato"
- value: "catppuccin-mocha"
label: "Catppuccin Mocha"
- value: "dark-plus"
label: "Dark Plus"
- value: "dracula"
label: "Dracula Theme"
- value: "dracula-soft"
label: "Dracula Theme Soft"
- value: "everforest-dark"
label: "Everforest Dark"
- value: "everforest-light"
label: "Everforest Light"
- value: "github-dark"
label: "GitHub Dark"
- value: "github-dark-default"
label: "GitHub Dark Default"
- value: "github-dark-dimmed"
label: "GitHub Dark Dimmed"
- value: "github-dark-high-contrast"
label: "GitHub Dark High Contrast"
- value: "github-light"
label: "GitHub Light"
- value: "github-light-default"
label: "GitHub Light Default"
- value: "github-light-high-contrast"
label: "GitHub Light High Contrast"
- value: "houston"
label: "Houston"
- value: "laserwave"
label: "LaserWave"
- value: "light-plus"
label: "Light Plus"
- value: "material-theme"
label: "Material Theme"
- value: "material-theme-darker"
label: "Material Theme Darker"
- value: "material-theme-lighter"
label: "Material Theme Lighter"
- value: "material-theme-ocean"
label: "Material Theme Ocean"
- value: "material-theme-palenight"
label: "Material Theme Palenight"
- value: "min-dark"
label: "Min Dark"
- value: "min-light"
label: "Min Light"
- value: "monokai"
label: "Monokai"
- value: "night-owl"
label: "Night Owl"
- value: "nord"
label: "Nord"
- value: "one-dark-pro"
label: "One Dark Pro"
- value: "one-light"
label: "One Light"
- value: "plastic"
label: "Plastic"
- value: "poimandres"
label: "Poimandres"
- value: "red"
label: "Red"
- value: "rose-pine"
label: "Rosé Pine"
- value: "rose-pine-dawn"
label: "Rosé Pine Dawn"
- value: "rose-pine-moon"
label: "Rosé Pine Moon"
- value: "slack-dark"
label: "Slack Dark"
- value: "slack-ochin"
label: "Slack Ochin"
- value: "snazzy-light"
label: "Snazzy Light"
- value: "solarized-dark"
label: "Solarized Dark"
- value: "solarized-light"
label: "Solarized Light"
- value: "synthwave-84"
label: "Synthwave '84"
- value: "tokyo-night"
label: "Tokyo Night"
- value: "vesper"
label: "Vesper"
- value: "vitesse-black"
label: "Vitesse Black"
- value: "vitesse-dark"
label: "Vitesse Dark"
- value: "vitesse-light"
label: "Vitesse Light"

apiVersion: v1alpha1
kind: Setting
metadata:
name: shiki-settings
spec:
forms:
- group: config
label: 配置
formSchema:
- $formkit: select
name: themeLight
label: 亮色主题
value: "github-light"
<<: *theme-options
- $formkit: select
name: themeDark
label: 暗色主题
value: "github-dark"
<<: *theme-options
- $formkit: checkbox
name: useBuiltinStyle
label: 使用内置样式
value: true
help: 开启后将注入插件内置的代码块 css,关闭后将只使用主题提供的 css。

2 changes: 2 additions & 0 deletions src/main/resources/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ spec:
name: Halo
website: https://github.com/halo-dev
logo: logo.svg
configMapName: shiki-configmap
settingName: shiki-settings
homepage: https://github.com/halo-sigs/plugin-shiki#readme
repo: https://github.com/halo-sigs/plugin-shiki
issues: https://github.com/halo-sigs/plugin-shiki/issues
Expand Down
3 changes: 3 additions & 0 deletions theme-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
},
"devDependencies": {
"@types/node": "20"
},
"dependencies": {
"@shikijs/transformers": "^1.16.1"
}
}
Loading