first commit
This commit is contained in:
22
usr/plugins/MarkdownParse/LICENSE.md
Executable file
22
usr/plugins/MarkdownParse/LICENSE.md
Executable file
@@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Taylor Otwell
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
112
usr/plugins/MarkdownParse/Plugin.php
Executable file
112
usr/plugins/MarkdownParse/Plugin.php
Executable file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace TypechoPlugin\MarkdownParse;
|
||||
|
||||
require_once 'phar://' . __DIR__ . '/vendor.phar/MarkdownParse.php';
|
||||
|
||||
use Typecho\Plugin\PluginInterface;
|
||||
use Typecho\Widget\Helper\Form;
|
||||
use Widget\Options;
|
||||
|
||||
/**
|
||||
* 符合 CommonMark 和 GFM(GitHub-Flavored Markdown)规范的 Markdown 解析插件,强大而丰富的功能助你在不同平台上展现一致的出色
|
||||
*
|
||||
* @author mrgeneral
|
||||
* @package MarkdownParse
|
||||
* @version 2.6.0
|
||||
* @link https://www.chengxiaobai.cn/
|
||||
*/
|
||||
class Plugin implements PluginInterface
|
||||
{
|
||||
const RADIO_VALUE_DISABLE = 0;
|
||||
const RADIO_VALUE_AUTO = 1;
|
||||
const RADIO_VALUE_FORCE = 2;
|
||||
|
||||
const CDN_SOURCE_DEFAULT = 'baomitu';
|
||||
const CDN_SOURCE_MERMAID = [
|
||||
'jsDelivr' => 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs',
|
||||
'cdnjs' => 'https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs',
|
||||
'baomitu' => 'https://lib.baomitu.com/mermaid/10.7.0/mermaid.esm.min.mjs'
|
||||
];
|
||||
const CDN_SOURCE_MATHJAX = [
|
||||
'jsDelivr' => 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js',
|
||||
'cdnjs' => 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.min.js',
|
||||
'baomitu' => 'https://lib.baomitu.com/mathjax/latest/es5/tex-mml-chtml.min.js'
|
||||
];
|
||||
|
||||
public static function activate()
|
||||
{
|
||||
\Typecho\Plugin::factory('\Widget\Base\Contents')->markdown = [__CLASS__, 'parse'];
|
||||
\Typecho\Plugin::factory('\Widget\Base\Comments')->markdown = [__CLASS__, 'parse'];
|
||||
\Typecho\Plugin::factory('Widget_Archive')->footer = [__CLASS__, 'resourceLink'];
|
||||
}
|
||||
|
||||
public static function deactivate()
|
||||
{
|
||||
// TODO: Implement deactivate() method.
|
||||
}
|
||||
|
||||
public static function config(Form $form)
|
||||
{
|
||||
$elementToc = new Form\Element\Radio('is_available_toc', [self::RADIO_VALUE_DISABLE => _t('不解析'), self::RADIO_VALUE_AUTO => _t('解析')], self::RADIO_VALUE_AUTO, _t('是否解析 [TOC] 语法(符合 HTML 规范,无需 JS 支持)'), _t('开会后支持 [TOC] 语法来生成目录'));
|
||||
$form->addInput($elementToc);
|
||||
|
||||
$elementMermaid = new Form\Element\Radio('is_available_mermaid', [self::RADIO_VALUE_DISABLE => _t('不开启'), self::RADIO_VALUE_AUTO => _t('开启(按需加载)'), self::RADIO_VALUE_FORCE => _t('开启(每次加载,pjax 主题建议选择此选项)')], self::RADIO_VALUE_AUTO, _t('是否开启 Mermaid 支持(支持自动识别,按需渲染,无需担心引入冗余资源)'), _t('开启后支持解析并渲染 <a href="https://mermaid-js.github.io/mermaid/#/">Mermaid</a>'));
|
||||
$form->addInput($elementMermaid);
|
||||
|
||||
$elementMermaidTheme = new Form\Element\Radio('mermaid_theme', ['default' => _t('默认(default)'), 'neutral' => _t('墨水(neutral)'), 'dark' => _t('暗黑(dark)'), 'forest' => _t('森林绿(forest)')], 'default', _t('Mermaid 主题颜色'), _t('可以去这里 <a href="https://mermaid.live/edit">实时编辑器</a>调整主题配置看下效果'));
|
||||
$form->addInput($elementMermaidTheme);
|
||||
|
||||
$elementMathJax = new Form\Element\Radio('is_available_mathjax', [self::RADIO_VALUE_DISABLE => _t('不开启'), self::RADIO_VALUE_AUTO => _t('开启(按需加载)'), self::RADIO_VALUE_FORCE => _t('开启(每次加载,pjax 主题建议选择此选项)')], self::RADIO_VALUE_AUTO, _t('是否开启 MathJax 支持(支持自动识别,按需渲染,无需担心引入冗余资源)'), _t('开启后支持解析并渲染 <a href="https://www.mathjax.org/">MathJax</a>'));
|
||||
$form->addInput($elementMathJax);
|
||||
|
||||
$elementCDNSource = new Form\Element\Radio('cdn_source', array_combine(array_keys(self::CDN_SOURCE_MERMAID), array_map('_t', array_keys(self::CDN_SOURCE_MERMAID))), self::CDN_SOURCE_DEFAULT, _t('静态资源 CDN'), _t('jsDelivr 默认使用最新版本'));
|
||||
$form->addInput($elementCDNSource);
|
||||
|
||||
$elementInternalHosts = new Form\Element\Text('internal_hosts', null, '', _t('设置内部链接'), _t('默认为本站点地址,支持正则表达式("/(^|\.)example\.com$/"),多个可用英文逗号分隔。<br/>外部链接解析策略:默认在新窗口中打开,并加上 "noopener noreferrer" 属性'));
|
||||
$form->addInput($elementInternalHosts);
|
||||
|
||||
$elementHelper = new Form\Element\Radio('show_help_info', [], self::RADIO_VALUE_DISABLE, _t('<a href="https://www.chengxiaobai.cn/php/markdown-parser-library.html/">点击查看更新信息</a>'), _t('<a href="https://www.chengxiaobai.cn/record/markdown-concise-grammar-manual.html/">点击查看语法手册</a>'));
|
||||
$form->addInput($elementHelper);
|
||||
}
|
||||
|
||||
public static function personalConfig(Form $form)
|
||||
{
|
||||
// TODO: Implement personalConfig() method.
|
||||
}
|
||||
|
||||
public static function parse($text)
|
||||
{
|
||||
$markdownParser = MarkdownParse::getInstance();
|
||||
|
||||
$markdownParser->setIsTocEnable((bool)Options::alloc()->plugin('MarkdownParse')->is_available_toc);
|
||||
$markdownParser->setInternalHosts((string)Options::alloc()->plugin('MarkdownParse')->internal_hosts ?: parse_url(Options::alloc()->siteUrl, PHP_URL_HOST));
|
||||
|
||||
return $markdownParser->parse($text);
|
||||
}
|
||||
|
||||
public static function resourceLink()
|
||||
{
|
||||
$markdownParser = MarkdownParse::getInstance();
|
||||
$configMermaid = (int)Options::alloc()->plugin('MarkdownParse')->is_available_mermaid;
|
||||
$configLaTex = (int)Options::alloc()->plugin('MarkdownParse')->is_available_mathjax;
|
||||
$configCDN = (string)Options::alloc()->plugin('MarkdownParse')->cdn_source;
|
||||
$isAvailableMermaid = $configMermaid === self::RADIO_VALUE_FORCE || ($markdownParser->getIsNeedMermaid() && $configMermaid === self::RADIO_VALUE_AUTO);
|
||||
$isAvailableMathjax = $configLaTex === self::RADIO_VALUE_FORCE || ($markdownParser->getIsNeedLaTex() && $configLaTex === self::RADIO_VALUE_AUTO);
|
||||
|
||||
$resourceContent = '';
|
||||
|
||||
if ($isAvailableMermaid) {
|
||||
$resourceContent .= sprintf('<script type="module">import mermaid from "%s";',self::CDN_SOURCE_MERMAID[$configCDN] ?: self::CDN_SOURCE_MERMAID[self::CDN_SOURCE_DEFAULT]);
|
||||
$resourceContent .= sprintf('mermaid.initialize({ startOnLoad: true,theme:"%s"});</script>', (string)Options::alloc()->plugin('MarkdownParse')->mermaid_theme ?: 'default');
|
||||
}
|
||||
|
||||
if ($isAvailableMathjax) {
|
||||
$resourceContent .= '<script type="text/javascript">(function(){MathJax={loader: {load: [\'[tex]/gensymb\']},tex:{inlineMath:[[\'$\',\'$\'],[\'\\\\(\',\'\\\\)\']],packages: {\'[+]\': [\'gensymb\']}}}})();</script>';
|
||||
$resourceContent .= '<script defer src="https://polyfill.alicdn.com/v3/polyfill.min.js?features=es6"></script>';
|
||||
$resourceContent .= sprintf('<script id="MathJax-script" defer type="text/javascript" src="%s"></script>', self::CDN_SOURCE_MATHJAX[$configCDN] ?: self::CDN_SOURCE_MATHJAX[self::CDN_SOURCE_DEFAULT]);
|
||||
}
|
||||
|
||||
echo $resourceContent;
|
||||
}
|
||||
}
|
||||
70
usr/plugins/MarkdownParse/README.md
Executable file
70
usr/plugins/MarkdownParse/README.md
Executable file
@@ -0,0 +1,70 @@
|
||||
Markdown Plugin for Typecho
|
||||
=========================
|
||||
|
||||
MarkdownParse 是一款基于 [league/commonmark](https://commonmark.thephpleague.com) 的 Typecho Markdown 解析插件,它的特色在于完美符合 [CommonMark](https://spec.commonmark.org) 和 GFM([GitHub-Flavored Markdown](https://github.github.com/gfm/))规范,不仅可以为你提供强大而丰富的功能,同时也能确保你的内容在不同平台上都能展现一致的出色效果。
|
||||
|
||||
本插件除了支持 CommonMark 和 GFM 规范内提到的功能(目录、表格、任务列表、脚标等等),MarkdownParse 还具有以下额外特性:
|
||||
|
||||
1. **Mermaid 语法支持:** 可以利用 Mermaid 语法轻松创建各种图表
|
||||
2. **MathJax 数学公式渲染:** 支持使用 MathJax 渲染数学公式
|
||||
3. **智能资源加载:** 根据实际渲染需求,能够智能识别是否加载渲染所需资源,无需担心引入冗余资源
|
||||
4. **图片延迟加载:** 支持浏览器原生的图片延迟加载技术,[MDN-Lazy loading](https://developer.mozilla.org/en-US/docs/Web/Performance/Lazy_loading)
|
||||
5. **文本高亮:** 通过 `<mark>` HTML 标签实现文本高亮效果,[MDN-Mark](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/mark)
|
||||
|
||||
## 环境要求
|
||||
|
||||
* Typecho 1.2.0 or higher
|
||||
* PHP 8.1 or higher
|
||||
|
||||
## 安装
|
||||
|
||||
1. [下载这个插件](https://github.com/mrgeneralgoo/typecho-markdown/releases)
|
||||
2. 修改文件夹的名字为 "MarkdownParse"
|
||||
3. 添加到你的项目中并启用它
|
||||
|
||||
## 配置页面
|
||||
|
||||

|
||||
|
||||
## 报告问题
|
||||
|
||||
[你可以直接点击这里提出你的问题](https://github.com/mrgeneralgoo/typecho-markdown/issues/new)
|
||||
|
||||
## 语法示例
|
||||
|
||||
https://www.chengxiaobai.cn/record/markdown-concise-grammar-manual.html
|
||||
|
||||
------
|
||||
|
||||
MarkdownParse is a Typecho Markdown parsing plugin based on [league/commonmark](https://commonmark.thephpleague.com). Its feature lies in its perfect compliance with [CommonMark](https://spec.commonmark.org) and GFM ([GitHub-Flavored Markdown](https://github.github.com/gfm/)) specifications. It not only provides you with powerful and abundant functions, but also ensures consistent outstanding effects of your content on different platforms.
|
||||
|
||||
In addition to the functions mentioned in the CommonMark and GFM specifications (table of contents, tables, task lists, footnotes, etc.), MarkdownParse also has the following additional features:
|
||||
|
||||
1. **Mermaid syntax support:** Easily create various charts using Mermaid syntax
|
||||
2. **MathJax formula rendering:** Supports rendering mathematical formulas using MathJax
|
||||
3. **Intelligent resource loading:** According to actual rendering needs, it can intelligently identify whether to load required rendering resources without worrying about introducing redundant resources
|
||||
4. **Image lazy loading:** Supports native image lazy loading technology in browsers, [MDN-Lazy loading](https://developer.mozilla.org/en-US/docs/Web/Performance/Lazy_loading)
|
||||
5. **Text highlight:** Realize text highlight effect through `<mark>` HTML tag, [MDN-Mark](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/mark)
|
||||
|
||||
## Requirements
|
||||
|
||||
* Typecho 1.2.0 or higher
|
||||
* PHP 8.1 or higher
|
||||
|
||||
## Installation
|
||||
|
||||
1. [Download this plugin](https://github.com/mrgeneralgoo/typecho-markdown/releases)
|
||||
2. Rename the folder to "MarkdownParse"
|
||||
3. Add it to your project and activate it
|
||||
|
||||
## Configuration
|
||||
|
||||

|
||||
|
||||
## Reporting Issues
|
||||
|
||||
[You can click here directly to create an issue](https://github.com/mrgeneralgoo/typecho-markdown/issues/new)
|
||||
|
||||
## Example
|
||||
|
||||
https://www.chengxiaobai.cn/record/markdown-concise-grammar-manual.html
|
||||
BIN
usr/plugins/MarkdownParse/vendor.phar
Executable file
BIN
usr/plugins/MarkdownParse/vendor.phar
Executable file
Binary file not shown.
Reference in New Issue
Block a user