我正在尝试使用两个参数( CSS类和Tweet )来生成一个短代码。
{{< tweet 877500564405444608 >}}
但是我想在post.md中像这样使用它
{{< tweet-single class="alignright" id="877500564405444608" >}}
若要生成html:
<div class="tweet-in-post alignright">
<twitter-widget class=....
</div>但是在tweet-single.html中使用这个
<!-- tweet-single -->
<div class="tweet-in-post {{ .Get "class" }}">
{{ (getJSON "https://api.twitter.com/1.1/statuses/oembed.json?dnt=1&hide_thread=1&id={{ .Get "id" }}") }}
</div>给出错误bad character U+0022 ‘"’。
这些文档https://gohugo.io/templates/data-templates/#call-the-functions-with-a-url展示了如何在Hugo中调用JSON函数,但我不知道如何使用这些示例来运行我的短代码。
发布于 2020-04-29 02:27:22
自定义内置短代码的最佳方法是从其代码开始。
您提供的链接显示了如何使用tweet短代码,但它没有显示其代码的实际外观。需要深入研究Hugo源代码,才能真正找到内置的短代码是什么样子的。
你可以在这里找到他们:https://github.com/gohugoio/hugo/tree/master/tpl/tplimpl/embedded/templates/shortcodes。
在当前提交(67f9204)中,这是twitter的短程序代码:
{{- $pc := .Page.Site.Config.Privacy.Twitter -}}
{{- if not $pc.Disable -}}
{{- if $pc.Simple -}}
{{ template "_internal/shortcodes/twitter_simple.html" . }}
{{- else -}}
{{- $url := printf "https://api.twitter.com/1/statuses/oembed.json?id=%v&dnt=%t" (index .Params 0) $pc.EnableDNT -}}
{{- $json := getJSON $url -}}
{{ $json.html | safeHTML }}
{{- end -}}
{{- end -}}这就是你的出发点。
但是这个例子特别棘手,因为这个短代码使用了twitter设置的样式,而且很难修改它。
This post可以在那里帮你。
这里的另一个困难是,如果您查看twitter的短代码,您可以看到twitter ID不是由{{ .Get "ID" }} (一个命名的params)调用的,而是由(index .Params 0) (一个位置params)调用的。雨果不让你把名字和位置搭配在一起。
因此,您不能在尝试时使用类似{{ .Get "class" }}的东西。相反,您需要使用位置参数(如{{ .Get xx }} ),xx是您的params在短代码调用中的位置。
这里有一个可能的解决方案:
{{- $pc := .Page.Site.Config.Privacy.Twitter -}}
{{- if not $pc.Disable -}}
{{- if $pc.Simple -}}
{{ template "_internal/shortcodes/twitter_simple.html" . }}
{{- else -}}
<blockquote class="twitter-tweet tw-align-{{ .Get 0 }}" data-lang="en">
{{- $url := printf "https://api.twitter.com/1/statuses/oembed.json?id=%v&dnt=%t" (index .Params 1) $pc.EnableDNT -}}
{{- $json := getJSON $url -}}
{{ $json.html | safeHTML }}
</blockquote>
{{- end -}}
{{- end -}}正如您所看到的,我在上面提到的帖子的答复之后添加了一些<blockquote>自定义,并且我使用{{ .Get 0 }}作为职位。这意味着,我现在必须将tweet的params位置调整为1。
如果将此短代码命名为tweet-single.html,则可以使用以下方法调用它:
{{< twitter-single right 877500564405444608>}}把推特放在右边。
要以推文为中心,应该是:
{{< twitter-single center 877500564405444608>}}请注意,right或center不使用引号,因为它们是位置参数。
替代方法:
如果由于某种原因这种方法不适用于您,那么另一种完全不同的方法是重新创建tweet的整个格式(而不使用twitter )。
如果您使用twitter_simple.html而不是twitter.html,Hugo就是这样做的。
如果您查看twitter_simple.html,您可以看到它们忽略了样式脚本,而是为.twitter-tweet类重新创建了一些简单的格式:
{{- $pc := .Page.Site.Config.Privacy.Twitter -}}
{{- $sc := .Page.Site.Config.Services.Twitter -}}
{{- if not $pc.Disable -}}
{{- $id := .Get 0 -}}
{{- $json := getJSON "https://api.twitter.com/1/statuses/oembed.json?id=" $id "&omit_script=true" -}}
{{- if not $sc.DisableInlineCSS -}}
{{ template "__h_simple_twitter_css" $ }}
{{- end -}}
{{ $json.html | safeHTML }}
{{- end -}}
{{ define "__h_simple_twitter_css" }}
{{ if not (.Page.Scratch.Get "__h_simple_twitter_css") }}
{{/* Only include once */}}
{{ .Page.Scratch.Set "__h_simple_twitter_css" true }}
<style type="text/css">
.twitter-tweet {
font: 14px/1.45 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;
border-left: 4px solid #2b7bb9;
padding-left: 1.5em;
color: #555;
}
.twitter-tweet a {
color: #2b7bb9;
text-decoration: none;
}
blockquote.twitter-tweet a:hover,
blockquote.twitter-tweet a:focus {
text-decoration: underline;
}
</style>
{{ end }}
{{ end }}您可以使用此代码,直到您有适合您的自定义。
https://stackoverflow.com/questions/61428211
复制相似问题