Giter VIP home page Giter VIP logo

Comments (5)

nilsnolde avatar nilsnolde commented on June 25, 2024

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.

from wordpress-markdown-git.

MIKEXU2017 avatar MIKEXU2017 commented on June 25, 2024

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/php_tool/)

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

class-base-loader and class-gitlab-loader.zip

from wordpress-markdown-git.

nilsnolde avatar nilsnolde commented on June 25, 2024

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:

  • can we make the markdown parser lib a subproject of this one? Preferably a git submodule but simply adding the classes with proper attribution would work too.
  • ideally this would be implemented for all providers so on the class-base-loader.php level
  • a slight problem can arise for private repos that get publicly published since images hosted on the provider's platform (e.g. when inserting images in the online markdown editor for github/gitlab/bitbucket) will need to be called with some form of authentication (which differs depending on the provider). However, solving that should probably be an entirely separate PR.

from wordpress-markdown-git.

MIKEXU2017 avatar MIKEXU2017 commented on June 25, 2024

@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

from wordpress-markdown-git.

nilsnolde avatar nilsnolde commented on June 25, 2024

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;)

from wordpress-markdown-git.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.