From 2820eda6a01bd4c095abfa8136d42c186929c954 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Mon, 31 May 2021 17:17:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BA=86=E4=BB=8E=E5=A4=96?= =?UTF-8?q?=E6=9D=A5=E6=BA=90=E5=8A=A0=E8=BD=BD=20markdown=20=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E5=92=8C=20md=20extra=20=E7=89=B9=E6=80=A7=EF=BC=8C?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8F=B7=E6=9B=B4=E6=96=B0=E4=B8=BA=200.2-de?= =?UTF-8?q?v?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将版本号修改为 0.2-dev - 添加了 标签声明外源引用文本 - 添加 marked.js 用于处理外源 markdown 资料 - 添加了自定义的 `$(xx)` 用于在 md 中声明外源资料 - 添加了配套的 ref.css 和 ref.js - 同时更新了 pjax 支持 - 将 Parsedown 更新为 ParsedownExtra --- assets/gitbook-fix.js | 2 ++ assets/ref.css | 6 ++++++ assets/ref.js | 15 +++++++++++++++ constant.php | 2 +- index.php | 4 ++-- src/Data/SiteMeta.php | 3 +++ src/Utils/ParsedownExtend.php | 32 ++++++++++++++++++++++++++++++++ 7 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 assets/ref.css create mode 100644 assets/ref.js create mode 100644 src/Utils/ParsedownExtend.php diff --git a/assets/gitbook-fix.js b/assets/gitbook-fix.js index 41874cc..6f0bc45 100644 --- a/assets/gitbook-fix.js +++ b/assets/gitbook-fix.js @@ -163,6 +163,8 @@ function updatePage (bookId, pageId = "") { // history window.history.pushState(document.documentElement.innerHTML, document.title, url); pageContainer.classList.remove("loading"); + // post-process + updateRef(); } } diff --git a/assets/ref.css b/assets/ref.css new file mode 100644 index 0000000..9b6ac88 --- /dev/null +++ b/assets/ref.css @@ -0,0 +1,6 @@ +ref { + display: block; + border: 2px solid var(--color-graystyle); + padding: 1em 2em; + margin: 1.7em 0 +} \ No newline at end of file diff --git a/assets/ref.js b/assets/ref.js new file mode 100644 index 0000000..1e063ca --- /dev/null +++ b/assets/ref.js @@ -0,0 +1,15 @@ +function updateRef () { + for (let node of document.getElementsByTagName("ref")) { + node.innerHTML = "..."; + const request = new XMLHttpRequest(); + request.open("GET", node.getAttribute("source"), true); + request.onreadystatechange = function () { + if (request.readyState === 4 && request.status === 200) { + node.innerHTML = marked(request.responseText); + } else if (request.readyState === 4) { + node.innerHTML = "ERROR "+ request.status; + } + } + request.send(); + } +} updateRef(); diff --git a/constant.php b/constant.php index c9c4e01..c9e96e8 100644 --- a/constant.php +++ b/constant.php @@ -2,7 +2,7 @@ const APP_NAME = "ph-Bookshelf"; -const VERSION = "0.1-dev"; +const VERSION = "0.2-dev"; const CHANNEL = "workshop-origin"; const BRANCH = "master"; diff --git a/index.php b/index.php index 28286a5..72b258b 100644 --- a/index.php +++ b/index.php @@ -3,12 +3,12 @@ require_once "./src/Data/SiteMeta.php"; require_once "./src/Data/PageMeta.php"; -require_once "./lib/Parsedown/Parsedown.php"; +require_once "./src/Utils/ParsedownExtend.php"; require_once "./src/Utils/PageParse.php"; require_once "./src/Utils/RequestNotExistException.php"; require_once "./constant.php"; -$parser = new Parsedown(); +$parser = new ParsedownExtend(); $parser->setMarkupEscaped(false); $parser->setSafeMode(false); diff --git a/src/Data/SiteMeta.php b/src/Data/SiteMeta.php index 65a1fe3..6f27882 100644 --- a/src/Data/SiteMeta.php +++ b/src/Data/SiteMeta.php @@ -31,6 +31,7 @@ class SiteMeta { "/assets/gitbook/style.css", "/assets/gitbook/gitbook-plugin-fontsettings/website.css", "/assets/gitbook-fix.css", + "/assets/ref.css", ); } @@ -38,6 +39,8 @@ class SiteMeta { return array( "/assets/gitbook/gitbook.js", "/assets/gitbook-fix.js", + "https://cdn.jsdelivr.net/npm/marked/marked.min.js", + "/assets/ref.js", ); } diff --git a/src/Utils/ParsedownExtend.php b/src/Utils/ParsedownExtend.php new file mode 100644 index 0000000..56df302 --- /dev/null +++ b/src/Utils/ParsedownExtend.php @@ -0,0 +1,32 @@ +InlineTypes['$'][] = 'RefMarkdown'; + + $this->inlineMarkerList .= '$'; + + } + + protected function inlineRefMarkdown ($excerpt) { + if (preg_match('/^\$\(([\S ]*?)\)/', $excerpt['text'], $matches)) { + return array( + 'extent' => strlen($matches[0]), + 'element' => array( + 'name' => 'ref', + 'text' => '', + 'attributes' => array( + 'source' => $matches[1], + ), + ), + + ); + } + } + +}