写日志流程再优化 2 - V2EX
请不要在回答技术问题时复制粘贴 AI 生成的内容
rwecho

写日志流程再优化 2

  •  1
     
  •   rwecho Jan 9, 2024 1983 views
    This topic created in 855 days ago, the information mentioned may be changed or developed.

    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 如下:

    这里的 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 replies    2024-01-11 15:31:54 +08:00
    hanbin
        1
    hanbin  
       Jan 9, 2024   1
    图挂了,切了新加坡和香港两个节点都出不来图,应该是挂了。
    rwecho
        2
    rwecho  
    OP
       Jan 9, 2024 via Android
    真是的,本来还想展示下 nextjs 做代理的用法
    rwecho
        3
    rwecho  
    OP
       Jan 10, 2024
    @hanbin 本来想用 sharp 组件裁剪图片的质量, 但是发布到 vercel 之后, 不兼容, 还没找到原因
    hanbin
        4
    hanbin  
       Jan 11, 2024
    @rwecho 现在有图了,是找到原因了,还是换解法了。
    About     Help     Advertise     Blog     API     FAQ     Solana     3243 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 36ms UTC 13:53 PVG 21:53 LAX 06:53 JFK 09:53
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86