Add InterWikiLink

This commit is contained in:
2025-12-14 08:14:58 +09:00
parent 0169766fb9
commit 4f58eddfda
10 changed files with 136 additions and 0 deletions

View File

@@ -55,3 +55,9 @@ gnome:
recent: 10
prompts-path: "./prompts.yaml"
links:
- key: "Code"
url: "https://link.example.org/code/"
- key: "GitHub"
url: "https://link.example.org/github/"

View File

@@ -7,6 +7,11 @@ import (
"gopkg.in/yaml.v3"
)
type Link struct {
Key string `yaml:"key"`
URL string `yaml:"url"`
}
type Config struct {
App struct {
Mode string `yaml:"mode"`
@@ -71,6 +76,8 @@ type Config struct {
PromptsPath string `yaml:"prompts-path"`
Prompts *Prompts
Links []Link `yaml:"links"`
}
func Load(path string) *Config {

View File

@@ -38,6 +38,8 @@ type Public struct {
}
Prompts Prompts
Links []Link
}
func Publish(cfg *Config) Public {
@@ -104,5 +106,7 @@ func Publish(cfg *Config) Public {
},
Prompts: *cfg.Prompts,
Links: cfg.Links,
}
}

View File

@@ -441,6 +441,38 @@ func link(s *state) bool {
return true
}
func interLink(s *state) bool {
line := s.input[s.index:s.lineEnd]
for _, item := range s.config.links {
if strings.HasPrefix(line, item.Key + ":") {
end := nonURLIndex(line[len(item.Key) + 1:])
rawURL := line[:len(item.Key) + 1 + end]
_, err := url.Parse(rawURL[len(item.Key) + 1:])
if err != nil {
continue
}
s.text.WriteString(rawURL)
s.plain.WriteString(rawURL)
htmlURL := template.HTMLEscapeString(rawURL)
htmlPath := template.HTMLEscapeString(rawURL[len(item.Key) + 1:])
s.html.WriteString("<a href=\"")
s.html.WriteString(item.URL)
s.html.WriteString(htmlPath)
s.html.WriteString("\" class=\"link\">")
s.html.WriteString(htmlURL)
s.html.WriteString("</a>")
s.index += len(rawURL)
return true
}
}
return false
}
func html(s *state) bool {
c := s.input[s.index]
if c == '&' {
@@ -487,6 +519,8 @@ func handleLine(s *state) {
continue
} else if em(s) {
continue
} else if interLink(s) {
continue
} else if camel(s) {
continue
} else if wikiLink(s) {

View File

@@ -11,11 +11,13 @@ type imageConfig struct {
type formatConfig struct {
image imageConfig
links []config.Link
}
func ToFormatConfig(cfg *config.Config) formatConfig {
fc := formatConfig{}
fc.image.domains = cfg.Image.Domains
fc.image.extensions = cfg.Image.Extensions
fc.links = cfg.Links
return fc
}

View File

@@ -515,6 +515,38 @@ func link(s *state) bool {
return true
}
func interLink(s *state) bool {
line := s.input[s.index:s.lineEnd]
for _, item := range s.config.links {
if strings.HasPrefix(line, item.Key + ":") {
end := nonURLIndex(line[len(item.Key) + 1:])
rawURL := line[:len(item.Key) + 1 + end]
_, err := url.Parse(rawURL[len(item.Key) + 1:])
if err != nil {
continue
}
s.text.WriteString(rawURL)
s.plain.WriteString(rawURL)
htmlURL := template.HTMLEscapeString(rawURL)
htmlPath := template.HTMLEscapeString(rawURL[len(item.Key) + 1:])
s.html.WriteString("<a href=\"")
s.html.WriteString(item.URL)
s.html.WriteString(htmlPath)
s.html.WriteString("\" class=\"link\">")
s.html.WriteString(htmlURL)
s.html.WriteString("</a>")
s.index += len(rawURL)
return true
}
}
return false
}
func html(s *state) bool {
c := s.input[s.index]
if c == '&' {
@@ -565,6 +597,8 @@ func handleLine(s *state) {
continue
} else if altEm(s) {
continue
} else if interLink(s) {
continue
} else if camel(s) {
continue
} else if wikiLink(s) {

View File

@@ -11,11 +11,13 @@ type imageConfig struct {
type formatConfig struct {
image imageConfig
links []config.Link
}
func ToFormatConfig(cfg *config.Config) formatConfig {
fc := formatConfig{}
fc.image.domains = cfg.Image.Domains
fc.image.extensions = cfg.Image.Extensions
fc.links = cfg.Links
return fc
}

View File

@@ -447,6 +447,38 @@ func link(s *state) bool {
return true
}
func interLink(s *state) bool {
line := s.input[s.index:s.lineEnd]
for _, item := range s.config.links {
if strings.HasPrefix(line, item.Key + ":") {
end := nonURLIndex(line[len(item.Key) + 1:])
rawURL := line[:len(item.Key) + 1 + end]
_, err := url.Parse(rawURL[len(item.Key) + 1:])
if err != nil {
continue
}
s.text.WriteString(rawURL)
s.plain.WriteString(rawURL)
htmlURL := template.HTMLEscapeString(rawURL)
htmlPath := template.HTMLEscapeString(rawURL[len(item.Key) + 1:])
s.html.WriteString("<a href=\"")
s.html.WriteString(item.URL)
s.html.WriteString(htmlPath)
s.html.WriteString("\" class=\"link\">")
s.html.WriteString(htmlURL)
s.html.WriteString("</a>")
s.index += len(rawURL)
return true
}
}
return false
}
func html(s *state) bool {
c := s.input[s.index]
if c == '&' {
@@ -493,6 +525,8 @@ func handleLine(s *state) {
continue
} else if em(s) {
continue
} else if interLink(s) {
continue
} else if camel(s) {
continue
} else if wikiLink(s) {

View File

@@ -11,11 +11,13 @@ type imageConfig struct {
type formatConfig struct {
image imageConfig
links []config.Link
}
func ToFormatConfig(cfg *config.Config) formatConfig {
fc := formatConfig{}
fc.image.domains = cfg.Image.Domains
fc.image.extensions = cfg.Image.Extensions
fc.links = cfg.Links
return fc
}

View File

@@ -155,6 +155,17 @@ Gnome =
<pre>{{.Public.Prompts.Gnome}}</pre>
</p>
<h3>Links</h3>
{{range .Public.Links}}
<p>
{{.Key}} = {{.URL}}
</p>
{{else}}
<p>
(none)
</p>
{{end}}
</main>
<footer class="menu">
<br />