This usually doesn’t happen if we set Typora to autosave in our preferences.

But today, when I was typing notes on Typora, my computer suddenly went black. I was worried that the last thing I typed might not be saved, but when I rebooted my computer, boy, the markdown file I typed was empty. The problem is far worse than I feared…

The volume of the original document has been very large, it is impossible to re-type, so we can only find a way to find the backup in the computer. Open file Manager to find the directory

C:\Users\ User name \AppData\Roaming\Typora\draftsRecoverCopy the code

Turns out here Typora saved us a copy of the file by date

I rushed to find my lost document, only to find that the image path here had changed to an absolute path. And there are many that can’t be located at all (many files in this directory are periodically purged)

My friends who use Typora to write Markdown documents often set their local images as relative paths, and I did the same, so we restored images for this scenario

Here we use a script to restore, the Java code is as follows

import java.io.*; import java.util.Scanner; /** * @author: pixel-revolve * @date: 2022/2/18 11:42 */ public class MdUtil { public static void main(String[] args) throws IOException { mdConverter(); } public static void mdConverter() throws IOException { Scanner scanner=new Scanner(System.in); System.out.println(" Please place the file to be processed under D:/work/ "); System.out.println(" Please enter the name of the file to be processed "); String fileName=scanner.next(); String dir="D:\work\"+fileName+".md"; System.out.println(" Please enter the name of the new file output after processing "); String newFileName=scanner.next(); File f1=new File(dir); If (f1.exists()) {system.out.println (" file already exists "); System.out.println(" Please select the script to execute 1: English document, 2: Chinese document, 3: English document + solve the absolute path to relative path problem "); String text = scanner.next(); Switch (text){case "1":{system.out.println (" start executing script 1"); script1(f1, newFileName); break; } case "2":{system.out.println (" start executing script 2"); script2(f1,newFileName); break; } case "3":{system.out.println (" start executing script 3"); script3(f1,newFileName); } } }else { try { f1.createNewFile(); System.out.println(" file created successfully "); } catch (Exception e) {// TODO: handle Exception system.out.println (" file creation failed "); }} system.out.println (" file already exists :"+f1.exists()); System.out.println(" file name :"+f1.getName()); System.out.println(" file path :"+f1.getPath()); System.out.println(" Absolute file path :"+ f1.getabsolutePath ()); System.out.println(" isDirectory :"+ f1.isdirectory ()); System.out.println(" file size :"+f1.length()); } /** * public static void script1(File File,String newFileName) throws IOException { File f2=new File(newFileName+".md"); // Create a BufferedWriter object and write to the file. BufferedWriter bw = new BufferedWriter(new FileWriter(f2)); Br = new BufferedReader(new FileReader(file)); String line; while ((line=br.readLine())! =null) { System.out.println(line); if (line.contains("! [image-")&&!line.contains("gitee.com")){ int index=line.lastIndexOf("](")+1; StringBuffer newLine=new StringBuffer(); for (int i = 0; i < line.length(); i++) { newLine.append(line.charAt(i)); if(i==index){ newLine.append("./"); } } bw.write(newLine.toString()+'\n'); }else { bw.write(line); bw.write('\n'); } } br.close(); // write("the second way to write and read"); bw.flush(); bw.close(); } /** ** the image inside the document... . Rewrite the preceding file name of assets to newFile * @param file * @param newFileName * @throws IOException */ public static void script2(file file,String newFileName) throws IOException { File f2=new File(newFileName+".md"); // Create a BufferedWriter object and write to the file. BufferedWriter bw = new BufferedWriter(new FileWriter(f2)); Br = new BufferedReader(new FileReader(file)); String line; while ((line=br.readLine())! =null) { System.out.println(line); if (line.contains("! [image-")&&!line.contains("gitee.com")){ int index=line.lastIndexOf("](")+1; int index2=line.lastIndexOf(".assets")-1; StringBuffer newLine=new StringBuffer(); for (int i = 0; i < line.length(); i++) { if(line.charAt(i)=='\'){ newLine.append('/'); }else { newLine.append(line.charAt(i)); } if(i==index){ newLine.append("./"); newLine.append(newFileName); i=index2; } } bw.write(newLine.toString()+'\n'); }else { bw.write(line); bw.write('\n'); } } br.close(); // write("the second way to write and read"); bw.flush(); bw.close(); @param file @param newFileName @throws IOException */ public static void script3(file file,String newFileName) throws IOException { Scanner scanner=new Scanner(System.in); System.out.println(" Please enter the absolute path in the picture "); String absoluteFilePath=scanner.nextLine(); File f2=new File(newFileName+".md"); // Create a BufferedWriter object and write to the file. BufferedWriter bw = new BufferedWriter(new FileWriter(f2)); Br = new BufferedReader(new FileReader(file)); String line; while ((line=br.readLine())! =null) { System.out.println(line); if (line.contains("! [image-")&&!line.contains("gitee.com")){ int index=line.lastIndexOf("](")+1; int index2=-1; if(line.contains(absoluteFilePath)) { index2 = line.lastIndexOf(absoluteFilePath)+absoluteFilePath.length()-1; } StringBuffer newLine=new StringBuffer(); for (int i = 0; i < line.length(); i++) { if(line.charAt(i)=='\'){ newLine.append('/'); }else { newLine.append(line.charAt(i)); } if(i==index){ newLine.append("./"); if(index2! =-1) { i = index2; } } } bw.write(newLine.toString()+'\n'); }else { bw.write(line); bw.write('\n'); } } br.close(); // write("the second way to write and read"); bw.flush(); bw.close(); }}Copy the code

Here, we use script 3 specifically for the operation of converting absolute path to relative path (other scripts can be seen if they are interested, because they are very simple for the operation of picture address). By default, create a work folder in disk D, put the MD documents to be repaired under this directory, and execute the script

The repaired MD file is generated in the project directory. In the preview of IDEA, you can see that our image path has been successfully converted into a relative path

Place this document in the same folder as your original. Assets folder. The images have been successfully located

Repair document work done! Unfortunately, the last modification before the screen went black was lost, but at least it was a lot better than starting all over again