What should I do if the external link of the picture fails? Use the npm tool to download/replace all invalid external links at once

Preface

About a month ago, the external link of Weibo pictures failed, and the Nuggets also lost their links due to hotlink issues.2019/06/06Decided to open anti-leech, The impact is:The personal blog website quoted these pictures outside the chain can not be displayed

Currently, the blocking of Weibo and Nuggets can be displayed normally in CSDN and segmentfault, which only affects personal blogs

For example, my blog some time ago:http://obkoro1.comNone of the Weibo pictures quoted above can be displayed.

Because I write blogs frequently, there are not a few blocked pictures, so I definitely can’t replace them manually. I checked and didn’t find a ready-made solution. I made a script tool and wrote a document to open it up.

markdown-img-down-site-change (download/replace the image in markdown)

Search the markdown file in the target folder, find the target picture, provide the function of downloading the picture, and replacing the picture link-usually used for markdown picture failure.

Introduction

This is an extremely lightweight script, reference the package, set the parameters, and easily get started through the API.

What problem is solved?

  1. Centrally download the pictures under a certain domain name in the markdown file to a folder.
  2. Replace the image link of a domain name in the markdown file with a new image link.
// 1. Download these two pictures
// ![](https://user-gold-cdn.xitu.io/2019/5/20/Picture name?w=2024&h=1240&f=png&s=339262)
// ![](https://user-gold-cdn.xitu.io/2018/6/16/picture name)
// 2. Replace with: github link
![](https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/Picture name?w=2024&h=1240&f=png&s=339262)
![](https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/Picture name)

installation:

npm i markdown-img-down-site-change -S

Documentation:

Github

API

Update log

Data Security:

You may not understand the function of the script when you are just getting started, and you need to debug it.markdownThe file is broken, don't you want to cry to death?

There are two forms of scripts to prevent this from happening:

  1. Script meetingDefault backupYour file.
  2. Default onTest mode, When the debugging is almost done, you can turn off the test mode.
  3. Suggestion: If you are not at ease, you can use one or two files to test the script

Use: Less than 20 lines of code

There is a use in the projectchestnut, There are a lot of comments and blank lines, the actual code is less than 20 lines, it can be said that it is very simple, as follows:

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

// Passing parameters: This is also the default parameter of the script, you can modify it according to the situation
let option = {
    replace_image_url: 'https://user-gold-cdn.xitu.io/',
    read_markdown_src: './source', // To find the folder address of the markdown file
    down_img_src: './juejin', // Download pictures 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/
}

// Initialize
const markdownImage = new markdownImageDown(option)

// Download external link
markdownImage.checkDownImg();

// Upload the downloaded image folder to the cloud, user operation

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

// Replace external link 
// Replace the string of replace_image_url with a string of new_image_url
markdownImage.replaceMarkdown();

run:

Read the text carefully and configure the parameters

Create a new one at the project root nodehandleImg.jsFile, install the script, and then usenodeRun the file:

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

Function/parameter introduction:

  • checkDownImg(): Download the found pictures
  • replaceMarkdown(): Replace the picture link with a new picture link
  • replace_image_url: The address of the picture to be replaced
  • new_image_url: The new address of the picture
  • test: Test mode.
  • var_number: Match the url before the image name of the image link, the value is the variable quantity
  • is_link: Match link.
  • write_file_time: How often to modify the markdown image link
  • read_markdown_src: To find the folder address of the markdown file
  • down_img_src:Download pictures to this address
  • copy_item_data: Backup project
  • filter_item: Filter some folders without searching markdown.
  • add_end: Add a suffix after the image link

Welcome to try

Friends in need, come and try! The document is well written and easy to get started. The project will continue to be maintained. If you have any questions, please contact meissue~

If you think this script is not bad, giveprojectOrder a Star!

BlogFront end accumulation documentNo public, Wx:OBkoro1, email: [email protected]

Above 2019.06.04

Intelligent Recommendation

IDEA---What should I do if the assertion fails?

IDEA solves assertion failure First of all, you have to go to IDEA to see if the assertion function is turned on? The assertion function needs to be turned on, it is turned off by default, and the met...

Qt4.8.1 Help document is invalid. Can't use? What should I do?

Qt is famous for his help documentation is very good, but in the natural state, after the qt4.8.1 is safe to use, even the input Widget can not find. Previously qt4.8.0, it is ok. No configuration is ...

What should I do if I import the external jar package but still get ClassNotFoundException?

One: Under the built web project, create a new lib folder, copy the external jar package used, and then correctly build-path, the unit test is completely fine, but after the project is released, an er...

[Turn] Gitee bed bed is blocked due to excessive access to the external link, what should we do

Reprinted in the whole article, the original link:Gitee bed bed is blocked because of too many external links, what should we do Articles directory 1. Event cause Second, plan selection 3. Resource re...

What should I do if the pip tool fails to install python third-party library?

Recently, I started learning Python and encountered the failure to install the python third-party library. Finally, I finally solved it with the help of Baidu. Here is how to successfully install pyth...

More Recommendation

What should I do with big screen data? All industry display templates are waiting for you to check (with links)

With the rapid development of informatization of the Internet of Things in recent years, the amount of data has also shown an exponential growth trend. Data visualization has gradually become the goal...

Unity Development Memo 000003: There is no Android option in External Tools, what should I do?

problem:The red box at the location below does not have an input box for adding JDK and SDK paths. We know that to package the android version of the application in Unity, you need to install the Java...

What should I do if we open the page in WeChat to stop access and cannot jump to an external browser

When we use WeChat marketing, it is easy to encounter the situation where the promotion link cannot be opened or the app cannot be downloaded in WeChat. First of all, let me talk about the problem tha...

Linux server setup, what should I do if the virtual machine linux in Windows cannot be connected to the external network?

1. Connect the computer's network cable port directly to the router's internal network interface (to ensure that the router can directly access the Internet normally, remember not to use broadband con...

Set an external breakpoint in the ABAP program, and the breakpoint cannot be triggered during debugging. What should I do?

A friend in my group asked this question: Everyone, please ask a question. The external breakpoint cannot be entered, and you need to set that place. Some enthusiastic friends in the group gave sugges...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top