Open TheOneWithTheBraid opened 4 years ago
Would be really nice, but this is so messy code-wise that I personally don't want to go down that road. You'd also have to consider images which are in private repos, which is already tricky (see #13 ) and would (at least for Bitbucket) not even be possible at all as the plugin can't create the token
needed to access the image. Not sure how that's handled with Github/-lab, didn't come up yet.
Also, when requesting the raw markdown from each provider, you'd have to parse the entire raw markdown string and pick out markdown image tags (![x](./x)
), check that there's no URL in there and process the path somehow so that it references the online resource instead and that process looks entirely different for each provider.
If you have a look into the code and you think it's doable in a reasonable fashion I'm happy to discuss or review a PR. Maybe there's even a workaround for the authentication issue.
Hi nilsnolde, I found a method that is more suitable for me, so I provide it here to see if it is suitable for adding to the plugin.
First, I get the content of the image and convert it to base64 encoding; Then, use a Markdown parser based on PHP language to convert MarkDown text to html text. Finally, I want to emphasize that I modified the set_repo_details function, because my url is http, not https, and at the same time, it is not the default port.
Installation steps of parsedown(_Put the decompressed file into documents-from-git/phptool/)
wget -O parsedown-1.7.4.tar.gz https://github.com/erusev/parsedown/archive/1.7.4.tar.gz
tar -zxvf parsedown-1.7.4.tar.gz
class-base-loader.php
// Block the content below.
// $args = array(
// 'body' => json_encode(array(
// "text" => $raw_markdown
// )),
// 'headers' => array(
// 'Content-Type' => 'application/json'
// )
// );
// // Add the Github credentials to have high /markdown rate limits
// $GITHUB_USER = MARKDOWNGIT_CONFIG['Github']['user'];
// $GITHUB_TOKEN = MARKDOWNGIT_CONFIG['Github']['token'];
// if (!empty($GITHUB_USER) or !empty($GITHUB_TOKEN)) {
// $args['headers']['Authorization'] = 'Basic ' . base64_encode($GITHUB_USER . ':' . $GITHUB_TOKEN);
// }
// $response = wp_remote_post(self::$GITHUB_MARKDOWN_API, $args);
// $html_body = wp_remote_retrieve_body($response);
// $html_string = '<div class="markdown-body">' . $html_body . '</div>';
// if ($this->is_static_cache() && $response_code == 200) {
// $this->set_content_cache($url,'markdown', $html_string);
// }
$current_plugin_file_abs_url = WP_PLUGIN_URL."/".dirname(plugin_basename(__FILE__));
$exploded_path = explode('//', $current_plugin_file_abs_url);
$exploded_path_last = explode('/', $exploded_path[1]);
$current_plugin_file_abs_path = ltrim($exploded_path[1], $exploded_path_last[0]);
require_once($_SERVER['DOCUMENT_ROOT'].$current_plugin_file_abs_path."/../../php_tool/parsedown-1.7.4/Parsedown.php");
$html_body = Parsedown::instance()->parse($raw_markdown);
$html_string = '<div class="markdown-body">' . $html_body . '</div>';
class-gitlab-loader.php
protected function parse_img_url(string $raw_url)
{
$url_parsed = parse_url($raw_url);
$scheme = $url_parsed['scheme'];
$domain = $url_parsed['host'];
$port = $url_parsed['port'];
$path = $url_parsed['path'];
$exploded_path = explode('/-/', $path);
$owner = ltrim($exploded_path[0], '/');
$owner = urlencode($owner);
$exploded_path_last = explode('/', $exploded_path[1]);
$branch = $exploded_path_last[1];
$file_path = implode('/', array_slice($exploded_path_last, 2));
$file_path = urlencode($file_path);
$target_url = "$scheme://$domain:$port/api/v4/projects/$owner/repository/files/$file_path/raw";
return $target_url;
}
protected function edit_image_link(string $content, array $args) {
$explode_path = end(explode('/', $this->path));
$abs_path = "$this->scheme://$this->domain:$this->port".explode($explode_path, $this->path)[0];
// $change_content = preg_replace('/(\!\[.+\]\()([\w\-\/\.]+)(\))/u', '$1'.$abs_path.'$2'.'$3', $content);
$change_content = $content;
$pattern = "/(\!\[.+\])\(([\w\-\/\.]+)\)/u";
preg_match_all($pattern, $change_content, $arr);
for($i = 0, $j = count($arr[0]); $i < $j; $i++) {
$raw_url = $abs_path.$arr[2][$i];
$target_url = $this->parse_img_url($raw_url);
$wp_remote = wp_remote_get($target_url, $args);
$response_body = wp_remote_retrieve_body($wp_remote);
$response_code = wp_remote_retrieve_response_code($wp_remote);
switch ($response_code) {
case 200:
$encode = base64_encode($response_body);
$change_content = str_replace($arr[0][$i], $arr[1][$i]."(data:image/jpeg;base64,".$encode.")", $change_content);
break;
case 404:
$raw_markdown = " # 404 - Not found.\n";
$change_content = str_replace($arr[0][$i], $arr[0][$i].$raw_markdown, $change_content);
break;
case 401:
$raw_markdown = " # 401 - Bad credentials.\n";
$change_content = str_replace($arr[0][$i], $arr[0][$i].$raw_markdown, $change_content);
break;
case 403:
$raw_markdown = " # 403 - Bad credentials.\n";
$change_content = str_replace($arr[0][$i], $arr[0][$i].$raw_markdown, $change_content);
break;
default:
$raw_markdown = " # 500 - Server Error.\n";
$change_content = str_replace($arr[0][$i], $arr[0][$i].$raw_markdown, $change_content);
}
}
return $change_content;
}
protected function get_document() {
$args = array(
'body' => array(
'ref' => $this->branch
)
);
if (!empty($this->token)) {
$args['headers']['Authorization'] = $this->get_auth_header();
}
$get_url = "$this->scheme://$this->domain:$this->port/api/v4/projects/$this->owner/repository/files/$this->file_path/raw";
$wp_remote = wp_remote_get($get_url, $args);
$response_body = wp_remote_retrieve_body($wp_remote);
$response_body = $this->edit_image_link($response_body, $args);
$response_code = wp_remote_retrieve_response_code($wp_remote);
return array($response_body, $response_code);
}
// Some modifications here are because my website is http://xxx: non-default port number
protected function set_repo_details(string $url)
{
$url_parsed = parse_url($url);
$scheme = $url_parsed['scheme'];
$domain = $url_parsed['host'];
$port = $url_parsed['port'];
$path = $url_parsed['path'];
$exploded_path = explode('/-/', $path);
$owner = ltrim($exploded_path[0], '/');
$exploded_path_last = explode('/', $exploded_path[1]);
$branch = $exploded_path_last[1];
$file_path = implode('/', array_slice($exploded_path_last, 2));
$this->domain = $domain;
$this->port = $port;
$this->scheme = $scheme;
$this->owner = urlencode($owner);
$this->branch = $branch;
$this->file_path = $file_path;
$this->path = $path;
}
Best Regards
Thanks @MIKEXU2017. I didn't really look at the code yet, as it's easier to do that in a PR setting.
Would you be interested in contributing a robust version of this functionality? Few things that'd come to mind for a PR:
git submodule
but simply adding the classes with proper attribution would work too.class-base-loader.php
level@nilsnolde Sorry, I have no experience in using git submodules, I only modified gitlab because I am using gitlab. I don't know if other things (such as github) will have problems. By the way, the token of the edit_image_link function is obtained from the get_document function, so as long as the token is given, there is no problem in obtaining the image of the private repo.
In fact, I just provide some suggestions, then tell you the changes I made, and finally improve your PR yourself because I want to be lazy. ( ^ _ ^ )
The file compression package (containing two modified files) has been placed in the previous comment.
Best Regards
Ok. No thanks, I have no interest in this functionality. However, I'd be happy to review PRs going in that direction for some future dev who's not as lazy;)
It would be great if you could process relative links like
[a link](./INSTALL.md)
and images to point to the original resource. Specially for images this is really important.Danke!