Typora把我的文档给吃了…

一般来说我们在偏好设置里设置Typora为自动保存的话是不会出现这种问题的。

image.png

但是今天使用Typora敲笔记的时候突然电脑黑屏了(poor windows)。原本还在担心会不会最后敲的东西没存上,结果重启电脑一看,好家伙,我之前敲的markdown文档整个空了。问题远比本人担心的要坏的多…

原来的文档的体量已经很大了,重敲是不可能重敲的,这样只能想办法找电脑中的备份了。
打开文件管理器找到该目录

C:\Users\用户名\AppData\Roaming\Typora\draftsRecover
复制代码

发现这里Typora按照日期给我们存了文档的备份

image.png
赶紧找到我丢失的文档,结果发现这里的图片路径转成了绝对路径了。而且还有好多根本定位不到(这个目录的很多文件是会定期清除的)

image.png
常用Typora写markdown文档的朋友一般会将本地图片设置成是相对路径,我也同样如此,于是我们针对这个场景进行图片恢复

image.png
这里我们使用脚本进行恢复,Java代码如下

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("请将要被处理的文件放到D:/work/下");
        System.out.println("请输入即将处理的文件的名字");
        String fileName=scanner.next();
        String dir="D:\work\"+fileName+".md";
        System.out.println("请输入处理后输出的新文件的名字");
        String newFileName=scanner.next();

        File f1=new File(dir);//相对路径,如果没有前面的src,就在当前目录创建文件
        if(f1.exists()) {
            System.out.println("文件已经存在");
            System.out.println("请选择需要执行的脚本  1:英文文档,2:中文文档,3:英文文档+解决绝对路径转成相对路径问题");
            String text = scanner.next();

            switch (text){
                case "1":{
                    System.out.println("开始执行脚本1");
                    script1(f1, newFileName);
                    break;
                }
                case "2":{
                    System.out.println("开始执行脚本2");
                    script2(f1,newFileName);
                    break;
                }
                case "3":{
                    System.out.println("开始执行脚本3");
                    script3(f1,newFileName);
                }
            }

        }else {
            try {
                f1.createNewFile();
                System.out.println("文件创建成功");
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("文件创建失败");
            }
        }
        System.out.println("文件已经存在:"+f1.exists());
        System.out.println("文件的名字:"+f1.getName());
        System.out.println("文件的路径:"+f1.getPath());
        System.out.println("文件的绝对路径:"+f1.getAbsolutePath());
        System.out.println("是目录吗:"+f1.isDirectory());
        System.out.println("文件大小:"+f1.length());
    }

    /**
     * 使用BuffredReader和BufferedWriter
     * 英文的文档不需要进行中文问题的处理
     */
    public static void script1(File file,String newFileName) throws IOException {
        File f2=new File(newFileName+".md");
        //创建BufferedWriter对象并向文件写入内容
        BufferedWriter bw = new BufferedWriter(new FileWriter(f2));

        //创建BufferedReader读取文件内容
        BufferedReader 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();

        // 向文件中写入内容
        // bw.write("the second way to write and read");
        bw.flush();
        bw.close();
    }

    /**
     * 需要对文档内部的图片。。。.assets的前面文件名进行改写成newFile
     * @param file
     * @param newFileName
     * @throws IOException
     */
    public static void script2(File file,String newFileName) throws IOException {
        File f2=new File(newFileName+".md");
        //创建BufferedWriter对象并向文件写入内容
        BufferedWriter bw = new BufferedWriter(new FileWriter(f2));

        //创建BufferedReader读取文件内容
        BufferedReader 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();

        // 向文件中写入内容
        // bw.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("请输入图片中的绝对路径");
        String absoluteFilePath=scanner.nextLine();
        File f2=new File(newFileName+".md");
        //创建BufferedWriter对象并向文件写入内容
        BufferedWriter bw = new BufferedWriter(new FileWriter(f2));

        //创建BufferedReader读取文件内容
        BufferedReader 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();

        // 向文件中写入内容
        // bw.write("the second way to write and read");
        bw.flush();
        bw.close();
    }


}
复制代码

这里我们使用脚本3专门针对绝对路径转相对路径的操作(其它的脚本有兴趣的可以看下,很简单都是针对图片地址的操作),默认在D盘下创建一个work文件夹,将需要修复的md文档放在该目录底下,执行脚本

image.png
在项目目录下生成了修复完成的md文档了,在idea的预览中可以看到我们的图片路径已经成功的转成相对路径了

image.png

将这个文档放到原来.assets文件夹的同级目录下,图片已经成功的定位到了

image.png

修复文档工作完成!可惜电脑黑屏前最后的一次修改内容丢掉了,但是至少是比全部重来要好太多了

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享