添加元数据渲染

This commit is contained in:
dragon
2025-11-26 14:12:03 +08:00
parent 4ce6949d3d
commit 11a42961f3
6 changed files with 49 additions and 29 deletions

View File

@@ -9,6 +9,7 @@ import (
"time"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/buffer"
"github.com/dop251/goja_nodejs/console"
"github.com/dop251/goja_nodejs/eventloop"
"github.com/dop251/goja_nodejs/require"
@@ -20,18 +21,14 @@ import (
func FilterInstGoJa(gl core.Params) (core.FilterInstance, error) {
var global struct {
Timeout time.Duration `json:"timeout"`
EnableDebug bool `json:"debug"`
EnableWebsocket bool `json:"websocket"`
EnableDebug bool `json:"debug"`
EnableWebsocket bool `json:"websocket"`
}
global.EnableDebug = true
global.EnableWebsocket = true
if err := gl.Unmarshal(&global); err != nil {
return nil, err
}
if global.Timeout == 0 {
global.Timeout = 60 * time.Second
}
return func(config core.Params) (core.FilterCall, error) {
var param struct {
Exec string `json:"exec"`
@@ -71,6 +68,10 @@ func FilterInstGoJa(gl core.Params) (core.FilterInstance, error) {
jsLoop.RunOnLoop(func(vm *goja.Runtime) {
err := func() error {
url.Enable(vm)
buffer.Enable(vm)
if err = MetaInject(ctx, vm); err != nil {
return err
}
if err = RequestInject(ctx, vm, request); err != nil {
return err
}

View File

@@ -0,0 +1,14 @@
package goja
import (
"github.com/dop251/goja"
"gopkg.d7z.net/gitea-pages/pkg/core"
)
func MetaInject(ctx core.FilterContext, jsCtx *goja.Runtime) error {
return jsCtx.GlobalObject().Set("meta", map[string]interface{}{
"org": ctx.Owner,
"repo": ctx.Repo,
"commit": ctx.CommitID,
})
}

View File

@@ -32,7 +32,13 @@ func FilterInstTemplate(_ core.Params) (core.FilterInstance, error) {
if err != nil {
return err
}
err = parse.Execute(out, utils.NewTemplateInject(request, nil))
err = parse.Execute(out, utils.NewTemplateInject(request, map[string]any{
"Meta": map[string]string{
"Org": ctx.Owner,
"Repo": ctx.Repo,
"Commit": ctx.CommitID,
},
}))
if err != nil {
return err
}