Skip to content

Commit

Permalink
新增功能
Browse files Browse the repository at this point in the history
  • Loading branch information
JaxsonWang committed Sep 16, 2019
1 parent cdc62ed commit 4b47f65
Show file tree
Hide file tree
Showing 8 changed files with 392 additions and 195 deletions.
371 changes: 188 additions & 183 deletions CHANGELOG.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions assets/version.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"author": "淮城一只猫",
"name": "WP Editor.md",
"version": "10.0.5",
"version": "10.0.6",
"assets": {
"ClipBoard": "2.0.4",
"CodeMirror": "5.48.4",
"Config": "10.0.5",
"Editormd": "10.0.5",
"Config": "10.0.6",
"Editormd": "10.0.6",
"Emojify.js": "1.1.0",
"jQuery": "1.12.4",
"KaTeX": "0.11.0",
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"require" : {
"php" : ">=5.3.0",
"michelf/php-markdown": "^1.8",
"league/html-to-markdown": "^4.8",
"league/html-to-markdown": "^4.8.2",
"jaxsonwang/wp-settings-api-class": "dev-master"
},
"homepage" : "https://github.com/JaxsonWang/WP-Editor.md/",
Expand Down
5 changes: 3 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ We recommend that you enable plugins in a clean environment (please disable othe

== Changelog ==

= 10.0.5 =
= 10.0.6 =

* 功能渲染转义问题修复
* 功能渲染转义问题修复
* 新增富文本文章转换 markdown 文章功能

注意:如果使用插件请不要使用Gutenberg编辑器,会出现文章数据丢失的问题。

Expand Down
46 changes: 42 additions & 4 deletions src/Admin/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,25 @@

namespace EditormdAdmin;

use EditormdApp\WPComMarkdown;
use League\HTMLToMarkdown\Converter\BlockquoteConverter;
use League\HTMLToMarkdown\Converter\CommentConverter;
use League\HTMLToMarkdown\Converter\DivConverter;
use League\HTMLToMarkdown\Converter\EmphasisConverter;
use League\HTMLToMarkdown\Converter\HardBreakConverter;
use League\HTMLToMarkdown\Converter\HeaderConverter;
use League\HTMLToMarkdown\Converter\HorizontalRuleConverter;
use League\HTMLToMarkdown\Converter\ImageConverter;
use League\HTMLToMarkdown\Converter\LinkConverter;
use League\HTMLToMarkdown\Converter\ListBlockConverter;
use League\HTMLToMarkdown\Converter\ListItemConverter;
use League\HTMLToMarkdown\Converter\ParagraphConverter;
use League\HTMLToMarkdown\Converter\PreformattedConverter;
use League\HTMLToMarkdown\Converter\TextConverter;
use League\HTMLToMarkdown\Environment;
use League\HTMLToMarkdown\HtmlConverter;
use EditormdApp\HTMLToMarkdownCodeConverter;
use EditormdApp\HTMLToMarkdownTableConverter;
use EditormdApp\WPComMarkdown;

class Controller {
/**
Expand Down Expand Up @@ -63,14 +80,35 @@ public function __construct() {
}

// 注入按钮
// add_action('post_submitbox_misc_actions', array($this, 'createMarkdownLink'));
// add_action('save_post', array($this, 'saveMarkdownMeta'), 10, 2);
add_action('post_submitbox_misc_actions', array($this, 'createMarkdownLink'));
add_action('save_post', array($this, 'saveMarkdownMeta'), 10, 2);

$this->htmlConverter = new HtmlConverter(

$environment = new Environment(
// your configuration here
array(
'header_style' => 'atx'
)
);

$environment->addConverter(new BlockquoteConverter());
$environment->addConverter(new HTMLToMarkdownCodeConverter); // optionally - add converter manually
$environment->addConverter(new CommentConverter());
$environment->addConverter(new DivConverter());
$environment->addConverter(new EmphasisConverter());
$environment->addConverter(new HardBreakConverter());
$environment->addConverter(new HeaderConverter());
$environment->addConverter(new HorizontalRuleConverter());
$environment->addConverter(new ImageConverter());
$environment->addConverter(new LinkConverter());
$environment->addConverter(new ListBlockConverter());
$environment->addConverter(new ListItemConverter());
$environment->addConverter(new ParagraphConverter());
$environment->addConverter(new PreformattedConverter());
$environment->addConverter(new TextConverter());
$environment->addConverter(new HTMLToMarkdownTableConverter()); // optionally - add converter manually

$this->htmlConverter = new HtmlConverter($environment);
}

/**
Expand Down
77 changes: 77 additions & 0 deletions src/App/HTMLToMarkdownCodeConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace EditormdApp;

use League\HTMLToMarkdown\Converter\ConverterInterface;
use League\HTMLToMarkdown\ElementInterface;

class HTMLToMarkdownCodeConverter implements ConverterInterface {
/**
* @param ElementInterface $element
*
* @return string
*/
public function convert(ElementInterface $element) {
$language = '';

// Checking for language class on the code block
$classes = $element->getAttribute('class');

if ($classes) {
// Since tags can have more than one class, we need to find the one that starts with 'language-'
$classes = explode(' ', $classes);
foreach ($classes as $class) {
if (strpos($class, 'language-') !== false) {
// Found one, save it as the selected language and stop looping over the classes.
$language = str_replace('language-', '', $class);
break;
}
}
}

$markdown = '';
$code = html_entity_decode($element->getChildrenAsString());

// In order to remove the code tags we need to search for them and, in the case of the opening tag
// use a regular expression to find the tag and the other attributes it might have
$code = preg_replace('/<code\b[^>]*>/', '', $code);
$code = str_replace('</code>', '', $code);

// Checking if it's a code block or span
if ($this->shouldBeBlock($element, $code)) {
// Code block detected, newlines will be added in parent
// $markdown .= '```' . $language . "\n" . $code . "\n" . '```';
$markdown .= '```' . $language . "\n" . $code . "" . '```';
} else {
// One line of code, wrapping it on one backtick, removing new lines
$markdown .= '`' . preg_replace('/\r\n|\r|\n/', '', $code) . '`';
}

return $markdown;
}

/**
* @return string[]
*/
public function getSupportedTags() {
return array('code');
}

/**
* @param ElementInterface $element
* @param string $code
*
* @return bool
*/
private function shouldBeBlock(ElementInterface $element, $code) {
if ($element->getParent()->getTagName() == 'pre') {
return true;
}

if (preg_match('/[^\s]` `/', $code)) {
return true;
}

return false;
}
}
76 changes: 76 additions & 0 deletions src/App/HTMLToMarkdownTableConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace EditormdApp;

use League\HTMLToMarkdown\Converter\ConverterInterface;
use League\HTMLToMarkdown\ElementInterface;

class HTMLToMarkdownTableConverter implements ConverterInterface
{
/**
* @param ElementInterface $element
*
* @return string
*/
public function convert(ElementInterface $element)
{
switch ($element->getTagName()) {
case 'tr':
$line = [];
$i = 1;
foreach ($element->getChildren() as $td) {
$i++;
$v = $td->getValue();
$v = trim($v);
if ($i % 2 === 0 || $v !== '') {
$line[] = $v;
}
}
return '| ' . implode(' | ', $line) . " |\n";

case 'td':
case 'th':
return trim($element->getValue());

case 'tbody':
return trim($element->getValue());

case 'thead':
$headerLine = reset($element->getChildren())->getValue();
$headers = explode(' | ', trim(trim($headerLine, "\n"), '|'));

$hr = [];
foreach ($headers as $td) {
$length = strlen(trim($td)) + 2;
$hr[] = str_repeat('-', $length > 3 ? $length : 3);
}
$hr = '|' . implode('|', $hr) . '|';

return $headerLine . $hr . "\n";
case 'table':
$inner = $element->getValue();
if (strpos($inner, '-----') === false) {
$inner = explode("\n", $inner);
$single = explode(' | ', trim($inner[0], '|'));
$hr = [];
foreach ($single as $td) {
$length = strlen(trim($td)) + 2;
$hr[] = str_repeat('-', $length > 3 ? $length : 3);
}
$hr = '|' . implode('|', $hr) . '|';
array_splice($inner, 1, 0, $hr);
$inner = implode("\n", $inner);
}
return trim($inner) . "\n\n";
}
return $element->getValue();
}

/**
* @return string[]
*/
public function getSupportedTags()
{
return array('table', 'tr', 'thead', 'td', 'tbody');
}
}
4 changes: 2 additions & 2 deletions wp-editormd.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WP Editor.md
* Plugin URI: https://github.com/JaxsonWang/WP-Editor.md
* Description: Perhaps this is the best and most perfect Markdown editor in WordPress
* Version: 10.0.5
* Version: 10.0.6
* Author: 淮城一只猫
* Author URI: https://iiong.com
* License: GPL-3.0+
Expand All @@ -18,7 +18,7 @@
use EditormdUtils\Activator;
use EditormdUtils\Deactivator;

define( 'WP_EDITORMD_VER', '10.0.5' ); //版本说明
define( 'WP_EDITORMD_VER', '10.0.6' ); //版本说明
define( 'WP_EDITORMD_URL', plugins_url( '', __FILE__ ) ); //插件资源路径
define( 'WP_EDITORMD_PATH', dirname( __FILE__ ) ); //插件路径文件夹
define( 'WP_EDITORMD_NAME', plugin_basename( __FILE__ ) ); //插件名称
Expand Down

0 comments on commit 4b47f65

Please sign in to comment.