V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
rwecho
V2EX  ›  程序员

写日志流程再优化 2

  •  1
     
  •   rwecho · 109 天前 · 1031 次点击
    这是一个创建于 109 天前的主题,其中的信息可能已经有所发展或是发生改变。

    https://www.v2ex.com/t/1006548

    上次分享了我的日志写作流程,虽然只有一个老哥看到了,但是提出了一些很好的建议,我也在这几天做了一些调整,现在的流程更加完善了。谢谢 @arloor

    前言

    上次提到可以使用 VSCode + OneDrive + Copilot 组合来写日志,但是有个问题,这里引用的图库是 imgur 和 unsplash 。这两个站点对于国内的访问速度都不是很理想。即使分发到了 mdnice 还是会有图片访问的问题。但是经老哥的提醒,我可以使用 VSCode 的 task 来做一点点事情。

    增加一个 mirror 图床

    因为我的网站是直接部署到 vercel 的,它本身在国内的访问速度尚可,所以可以利用它进行一次转发,从而实现国内的访问速度。看代码:

    import { NextRequest } from 'next/server'
    
    // handle the get request and return the response
    export async function GET(
      request: NextRequest,
      context: { params: { id: string } }
    ) {
      const url = request.nextUrl.searchParams.get('url') ?? ''
    
      if (!checkImageUrl(url)) return new Response('Invalid URL', { status: 400 })
    
      // download the image and return it
      const image = await fetch(url)
      const imageBuffer = await image.arrayBuffer()
      const imageType = image.headers.get('content-type') ?? 'image/jpeg'
    
      const response = new Response(imageBuffer, { status: 200 })
      response.headers.set('content-type', imageType)
      response.headers.set('cache-control', 'public, max-age=31536000, immutable')
      return response
    }
    
    function checkImageUrl(url: string) {
      return url.match(/https:\/\/i.imgur.com\/[a-zA-Z0-9]+/) ||
        url.match(/https:\/\/images.unsplash.com\/photo-[a-zA-Z0-9]+/)
        ? true
        : false
    }
    

    这样的话,就可以直接通过 api/i?url=https://i.imgur.com/QNV9QPz.png 进行访问了。速度还可以。 这里应该还有优化的空间,例如权限,例如缓存,例如图片压缩?

    这样就有了一个自己的图床,只有在日志里面引用 imgur 的图片,那就可以直接使用这个图床了。

    使用 task 对日志进行处理

    这里的 task 是指 VSCode 的 task ,可以在 VSCode 的菜单栏中找到,也可以通过快捷键 Ctrl + Shift + B 来打开,也就是上面老哥提到的方法。

    在上篇文章中提到,每次我的截图,通过 VSCode 的插件 vscode-imgur 来上传到 imgur ,然后直接粘贴到 markdown 中。还有 unsplash 的图片也是直接粘贴到 markdown 中。这样的话需要一个 format 的处理过程。task 如下:

    Image

    这里的 format.py 是一个 python 脚本,用来处理 markdown 文件,代码如下:

    
    # format the file with replace all imgur links with the vercel mirror
    # read the file
    with open(format_file, "r", encoding='utf-8') as f:
        lines = f.readlines()
    
        # regex ![]( https://rwecho.top/api/i?url=https://) to match the imgur links
        r = re.compile(r"!\[.*\]\(( https://.*)\)")
        for i, line in enumerate(lines):
            # check if the line matches the regex
            match = r.match(line)
            if not match:
                continue
            # get the link
            link = match.group(1)
    
            if (link.startswith(mirror)):
                continue
    
            # replace the link with the vercel mirror
            replaced_link = f"{mirror}{link}"
            lines[i] = line.replace(link, replaced_link)
            has_formatted = True
            print(f"Replaced {link} with mirror {replaced_link}")
    

    这样的话,每次可以通过执行 task ,完成 markdown 文件的格式化。另外我还顺便把英文标点符号替换成了中文标点符号。

    Image

    还有什么步骤可以优化的吗?

    4 条回复    2024-01-11 15:31:54 +08:00
    hanbin
        1
    hanbin  
       109 天前   ❤️ 1
    图挂了,切了新加坡和香港两个节点都出不来图,应该是挂了。
    rwecho
        2
    rwecho  
    OP
       109 天前 via Android
    真是的,本来还想展示下 nextjs 做代理的用法
    rwecho
        3
    rwecho  
    OP
       108 天前
    @hanbin 本来想用 sharp 组件裁剪图片的质量, 但是发布到 vercel 之后, 不兼容, 还没找到原因 😭
    hanbin
        4
    hanbin  
       107 天前
    @rwecho 现在有图了,是找到原因了,还是换解法了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   892 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:24 · PVG 04:24 · LAX 13:24 · JFK 16:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.