From 48c031ecd1fb646c7b498c0ca860a459278f05af Mon Sep 17 00:00:00 2001 From: litefeel Date: Mon, 6 Apr 2020 18:30:19 +0800 Subject: [PATCH] Fixed the url of files in the images directory --- writing-on-github.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/writing-on-github.php b/writing-on-github.php index 8cef207..cf16ca8 100644 --- a/writing-on-github.php +++ b/writing-on-github.php @@ -179,21 +179,28 @@ public function ignore_post_meta($meta) { public function the_content($content) { $arr = wp_upload_dir(); $baseurl = $arr['baseurl'] . '/writing-on-github'; + $basedir = $arr['basedir'] . '/writing-on-github'; $content = preg_replace_callback( - '/(]*?src=[\'"])\s*(\/images\/[^\s#]\S+)\s*([\'"][^>]*?>)/', - function($matchs) use ($baseurl) { - $url = $baseurl . $matchs[2]; - return "${matchs[1]}$url${matchs[3]}"; + '/(]*?src=[\'"])\S*?(\/images\/\S+)([\'"].*?>)/', + function($matchs) use ($baseurl, $basedir) { + if (is_file($basedir . $matchs[2])) { + $url = $baseurl . $matchs[2]; + return "${matchs[1]}$url${matchs[3]}"; + } + return "${matchs[0]}"; }, $content ); $content = preg_replace_callback( - '/(]*?href=[\'"])\s*(\/images\/[^\s#]\S+)\s*([\'"][^>]*?>)/', - function($matchs) use ($baseurl) { - $url = $baseurl . $matchs[2]; - return "${matchs[1]}$url${matchs[3]}"; + '/(]*?href=[\'"])\S*?(\/images\/S+)\s*([\'"].*?>)/', + function($matchs) use ($baseurl, $basedir) { + if (is_file($basedir . $matchs[2])) { + $url = $baseurl . $matchs[2]; + return "${matchs[1]}$url${matchs[3]}"; + } + return "${matchs[0]}"; }, $content );