Add InterWikiLink
This commit is contained in:
@@ -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/"
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 />
|
||||
|
||||
Reference in New Issue
Block a user