diff options
| author | BigCoke233 <hi@guhub.cn> | 2020-04-01 11:14:06 +0800 |
|---|---|---|
| committer | BigCoke233 <hi@guhub.cn> | 2020-04-01 11:14:06 +0800 |
| commit | afa631ff8486d614a387759bdc8b473b1534404f (patch) | |
| tree | 9120abac31f3edf1f2b5fcc5620c68ae4a8b3b80 | |
| parent | a474b01b276ba757c6aec5edc3cc87338b081d7f (diff) | |
:man: Add: 友链支持调用外部 json 数据
| -rw-r--r-- | assets/linkData.sample.json | 32 | ||||
| -rw-r--r-- | libs/Contents.php | 75 | ||||
| -rw-r--r-- | post.php | 2 |
3 files changed, 83 insertions, 26 deletions
diff --git a/assets/linkData.sample.json b/assets/linkData.sample.json new file mode 100644 index 0000000..6106451 --- /dev/null +++ b/assets/linkData.sample.json @@ -0,0 +1,32 @@ +[ + { + "name": "Eltrac", + "link": "https://guhub.cn/", + "avatar": "https://cdn.jsdelivr.net/gh/BigCoke233/image-hosting/avatars/avatar.jpeg", + "des": "暗夜行路,终遇奇迹。" + }, + { + "name": "Example", + "link": "https://example.io/", + "avatar": "https://cdn.jsdelivr.net/gh/BigCoke233/image-hosting/avatars/avatar.jpeg", + "des": "这是一个示例。" + }, + { + "name": "Example", + "link": "https://example.io/", + "avatar": "https://cdn.jsdelivr.net/gh/BigCoke233/image-hosting/avatars/avatar.jpeg", + "des": "这是一个示例。" + }, + { + "name": "Example", + "link": "https://example.io/", + "avatar": "https://cdn.jsdelivr.net/gh/BigCoke233/image-hosting/avatars/avatar.jpeg", + "des": "这是一个示例。" + }, + { + "name": "Example", + "link": "https://example.io/", + "avatar": "https://cdn.jsdelivr.net/gh/BigCoke233/image-hosting/avatars/avatar.jpeg", + "des": "这是一个示例。" + } +]
\ No newline at end of file diff --git a/libs/Contents.php b/libs/Contents.php index db9bfff..0b1eaf7 100644 --- a/libs/Contents.php +++ b/libs/Contents.php @@ -1,6 +1,7 @@ <?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; class Contents { + /** * 内容解析器入口 * 传入的是经过 Markdown 解析后的文本 @@ -85,6 +86,31 @@ class Contents </div>'; $text = preg_replace($reg,$rp,$text); + //解析外部友链 + $reg = '/\[links data="(.*?)"\]/s'; + $dataLink = preg_replace($reg,'${1}',$text); + $data = file_get_contents($dataLink); + $data = json_decode($data, true); + $linkItemNum = count($data); + for ($i = 0; $i < $linkItemNum; $i++) { + $name = $data[$i]['name']; + $link = $data[$i]['link']; + $avatar = $data[$i]['avatar']; + $des = $data[$i]['des']; + //嵌入列表 + $linksList .= '<div class="col-lg-2 col-6 col-md-3 links-container"> + <a href="'. $link .'" title="'. $des .'" target="_blank" class="links-link"> + <div class="links-item"> + <div class="links-img" style="background:url(\''. $avatar .'\');width: 100%;padding-top: 100%;background-repeat: no-repeat;background-size: cover;"></div> + <div class="links-title"> + <h4>'. $name .'</h4> + </div> + </div> + </a> + </div>'; + } + $text=preg_replace($reg,'<div class="links-box container-fluid"><div class="row">'. $linksList .'</div></div>',$text); + return $text; } @@ -133,37 +159,13 @@ class Contents } /** - * 解析自定义导航栏 - */ - static public function paresNav($data, $type) { - $de_json = json_decode($data, true); - $count_json = count($de_json); - for ($i = 0; $i < $count_json; $i++) { - $title = $de_json[$i]['title']; - $link = $de_json[$i]['link']; - //输出导航 - if($type="top-nav") { - echo '<a href="'. $link .'">'. $title .'</a>'; - } - elseif($type="mobile") { - echo '<div class="col-6"><a href="'. $link .'">'. $title .'</a></div>'; - } - elseif($type="drawer") { - echo '<a href="'. $link .'" onclick="toggleDrawer()">'. $title .'</a>'; - } - } - } - - /** * 解析代码块 */ static public function parseCode($text) { $text = preg_replace('/<pre><code>/s','<pre><code class="language-html">',$text); return $text; } - - //从这里开始的代码来自 熊猫小A (https://imalan.cn) - + /** * 解析章节链接 */ @@ -185,6 +187,29 @@ class Contents return '<h'.$matchs[1].$matchs[2].' id="'.$id.'">'.$matchs[3].'<a href="#'.$id.'" title="章节链接" class="post-toc-link no-line"><i class="iconfont icon-paragraph"></i></a></h'.$matchs[1].'>'; } + + /** + * 解析自定义导航栏 + */ + static public function paresNav($data, $type) { + $de_json = json_decode($data, true); + $count_json = count($de_json); + for ($i = 0; $i < $count_json; $i++) { + $title = $de_json[$i]['title']; + $link = $de_json[$i]['link']; + //输出导航 + if($type="top-nav") { + echo '<a href="'. $link .'">'. $title .'</a>'; + } + elseif($type="mobile") { + echo '<div class="col-6"><a href="'. $link .'">'. $title .'</a></div>'; + } + elseif($type="drawer") { + echo '<a href="'. $link .'" onclick="toggleDrawer()">'. $title .'</a>'; + } + } + } + /** * 根据 id 返回对应的对象 * 此方法在 Typecho 1.2 以上可以直接调用 Helper::widgetById(); @@ -13,7 +13,7 @@ $this->need('includes/header.php'); </div></div> </div> <?php endif; ?> - <?php if($this->options->ifShowRTA==0): ?> + <?php if($this->options->ifShowRTA!=0): ?> <div class="tip blue" id="reading-time-alert"> <div class="container-fluid"> <div class="row"> |
