Tianhe Gao

Hugo Add . Keyboard Shortcut

I read a post, like the idea of press . then take me to the edit page. I made some modifications to the code, here is mine:

 1  window.addEventListener("keypress", (event) => {
 2    const isEditKey = event.key === ".";
 3    const isHomePage = window.location.pathname === "/";
 4    const isPostsPage = window.location.pathname === "/posts/";
 5    const isTagsPage = window.location.pathname.startsWith("/tags/");
 6
 7    const isInputOrTextArea =
 8      event.target.nodeName === "INPUT" || event.target.nodeName === "TEXTAREA";
 9
10    if (
11      isEditKey &&
12      !isHomePage &&
13      !isPostsPage &&
14      !isTagsPage &&
15      !isInputOrTextArea
16    ) {
17      const contentPath = window.location.pathname.split("/");
18      const fileName =
19        contentPath.length === 3
20          ? `${contentPath[contentPath.length - 2]}.org`
21          : `posts/${contentPath[contentPath.length - 2]}.org`;
22      const editUrl = `https://github.com/tianheg/blog/edit/main/content/${fileName}`;
23      window.location.href = editUrl;
24    }
25  });

No notes link to this note

Welcome to tell me your thoughts via "email"
UP