preface

About a month ago, the external link of the pictures on weibo failed, and the Nuggets decided to open the anti-theft link on 2019/06/06 due to the problem of theft. The impact is that the external link of the personal blog website can not be displayed if these pictures are referenced.

The current blocking of Twitter and Nuggets, both in CSDN and segmentFault, is normal and only affects individual blogs.

For example, some time ago my blog, Obkoro1.com, referenced twitter images that were not displayed.

As I write blog frequently, there are many pictures that are blocked, so I can’t replace them manually one by one. After checking, I didn’t find a ready-made solution, so I made a script tool and wrote a document to open source it.

Markdown -img-down-site-change(download/replace images in Markdown)

Search the markdown file in the target folder, find the target image, provide the function to download the image, replace the image link – usually used for markdown image inactivation.

Introduction to the

This is a very lightweight script that references packages, sets parameters, and is easily accessible through the API.

Solve what problem?

  1. Download all images from a domain name in the Markdown file to a folder.
  2. Replace the image link for a domain name in the Markdown file with the new image link.
// 1. Download these two images
/ /! [](https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2019/5/20/~tplv-t2oaga2asx-image.image picture name? W =2024&h=12 40&f=png&s=339262)
/ /! [](https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/6/16/~tplv-t2oaga2asx-image.image photo name)
// 2. Replace with: github link! [](https:/ / raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/ image name? W = 2024 & h = 1240 & f = png&s = 339262)! [](https:/ / raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/ image name)
Copy the code

Installation:

npm i markdown-img-down-site-change -S
Copy the code

Documents:

Github

API

Update log

Data security:

You may not understand the function of the script at first, so you need to debug it. At this time, if you change the Markdown file, you will cry.

Scripts take two forms to prevent this from happening:

  1. The script will back up your files by default.
  2. Test mode is turned on by default, and can be turned off after debugging.
  3. Suggestion: If you’re worried, test the script with a file or two

Usage: less than 20 lines of code

In the project, there is a use of chestnut, which added a lot of comments and blank lines, the actual code is less than 20 lines, can be said to be very simple, as follows:

// npm i markdown-img-down-site-change -S 
const markdownImageDown = require('markdown-img-down-site-change'); // File module

// Pass parameters: This is also the default parameter of the script, which can be modified as required
let option = {
    replace_image_url: 'https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/~tplv-t2oaga2asx-image.image'.read_markdown_src: './source'.// To find the folder address of the markdown file
    down_img_src: './juejin'.// Download images to this folder
    var_number: 3 // The number of variables in the first half of the URL such as the date above: /2019/5/20/, /2018/6/16/
}

/ / initialization
const markdownImage = new markdownImageDown(option)

// Download the external link
markdownImage.checkDownImg();

// Upload the downloaded image folder to the cloud user's own operation

// After uploading the image
// The script will replace the previous external link with the cloud address + splice a picture name
markdownImage.updateOption({
    new_image_url: 'https://xxx.com/ directory address /'.// The address to upload the image
    add_end: '? raw=true' // The github image address has the suffix directly into the repository
})

// Replace the outer chain
// Replace the replace_image_URL string with the new_image_URL string
markdownImage.replaceMarkdown();
Copy the code

Run:

Read the text carefully and set the parameters

Create a new handleim. js file at the root of the project, install the script, and run the file with Node:

npm i markdown-img-down-site-change -S
node handleImg.js
Copy the code

Welcome to try

Have the need of small partners, hurry to try it! The document is very complete, very easy to use, the project will continue to maintain, if you have any questions, please send me an issue~

If you think this script is good, give the project a Star!

Front-end advanced accumulation, official account, WX :OBkoro1, email: [email protected]

The above 2019.06.04