Open2

Obsidian コード検索メモ

knaka Tech-Blogknaka Tech-Blog

概要

  • Obsidian検索で .md(マークダウン) 以外のファイル検索調査メモです。
  • Obsidian 初心者ですが。 mdファイル以外 検索が難しいようでした。
  • 検索すると。プラグインでファイル検索できそうでしたが。よくわからず。。でした
  • ファイル拡張子を。(例 .js)name.js.md みたいなファイル名に変更すると検索できました。
  • コードの場合 、MDファイルに、コードブロック内にコード書くのが普通らしいです。

[ 公開 2025/08/26 ]


環境

  • Obsidian windows 1.9.12
  • node 22

関連


書いたコード

https://gist.github.com/kuc-arc-f/b1db41459f6712675a589ce30d21491f

  • windows版
  • ファイル数が多い場合、拡張子変更が多く時間かかりそうで。厳しいようです。
  • 今回は試しで、アプリ数 約30、各フォルダのファイル30~50前後
  • node.jsで、拡張子変更(name.js.md) 処理作成、実行

  • main.js
  • ファイル名変換フォルダの設定
  • Obsidian vault 内に、検索等フォルダ作成、検索ファイルをコピーする
const TARGET_PATH = "/path/Obsidian/vault/codevault"

  • config.js
  • 変換対象の拡張子
export const EXT_TIME = [
  /* JS */
  ".js",
  ".jsx",
  ".ts",
  ".tsx",
  ".json",
  ".css",
  ".htm",
  ".html",
  ".vue",
  ".svelte",
  /* py */
  ".py",
  /* GO */
  ".go",
  /* RUST */
  ".rs",
  /* PHP */
  ".php",

];

  • ファイル名変換、実行
npm run start

  • main.js 全体
import fs from "fs";
import path from "path";


import { EXT_TIME } from "./config.js";
const TARGET_PATH = "/path/Obsidian/vault/codevault"

/**
*
* @param
*
* @return
*/
function getAllFiles(dirPath, arrayOfFiles = []) {
  const files = fs.readdirSync(dirPath);

  for (const file of files) {
    const fullPath = path.join(dirPath, file);
    if (fs.statSync(fullPath).isDirectory()) {
      // 再帰で探索
      getAllFiles(fullPath, arrayOfFiles);
    } else {
      arrayOfFiles.push(fullPath);
    }
  }

  return arrayOfFiles;
}

/**
*
* @param
*
* @return true: OK, false: NG
*/
const validExtention = function(ext){
  let ret = false;
  ret = EXT_TIME.includes(ext);
//  console.log(ret);
  return ret;
}
/**
*
* @param
*
* @return
*/
function main(){
  try{
    // 例: test フォルダ内の全ファイルパスを取得 TARGET_PATH
    const folderPath = path.resolve(TARGET_PATH); // 絶対パスに変換
    const files = getAllFiles(folderPath);

    //console.log(files);
    for (const file of files) {
      let vdir = fs.statSync(file).isDirectory();
      if(!vdir){
        console.log("vdir=", file);
        let pathObject = path.parse(file);
        let fileName = pathObject.base;
        //console.log(fileName);
        let ext = pathObject.ext;
        console.log("ext=", ext);
        let vExt = validExtention(ext);
        console.log("vExt=", vExt);
        if(ext !== ".md" && vExt){
          let targetDir = pathObject.dir;
          let targetFnm = targetDir + "\\" + fileName+ ".md";
          console.log("chgname=", targetFnm);
          fs.renameSync(file, targetFnm);
        }
      }

    }
  }catch(e){ console.log(e);}
}
//console.log(EXT_TIME);
main();

  • tree
  • ファイル変換後、一部
$ tree .
.
├── README.md
├── package.json.md
├── public
│   ├── favicon.ico
│   ├── index.html.md
│   ├── logo192.png
│   ├── logo512.png
│   ├── manifest.json.md
│   └── robots.txt
├── src
│   ├── App.css.md
│   ├── App.test.tsx.md
│   ├── App.tsx.md
│   ├── apollo-client.ts.md
│   ├── component
│   │   ├── Home.tsx.md
│   │   ├── Navbar.tsx.md
│   │   ├── Test.tsx.md
│   │   ├── login.tsx.md
│   │   ├── notes
│   │   │   ├── Create.tsx.md
│   │   │   ├── Edit.tsx.md
│   │   │   ├── Index.tsx.md
│   │   │   ├── IndexRow.tsx.md
│   │   │   └── Show.tsx.md
│   │   ├── tasks
│   │   │   ├── Create.tsx.md
│   │   │   ├── Edit.tsx.md
│   │   │   ├── Index.tsx.md
│   │   │   ├── IndexRow.tsx.md
│   │   │   └── Show.tsx.md
│   │   └── user
│   │       └── Create.tsx.md
│   ├── graphql
│   │   ├── note.ts.md
│   │   ├── task.ts.md
│   │   └── user.ts.md
│   ├── index.css.md
│   ├── index.tsx.md
│   ├── lib
│   │   ├── LibConfig.ts.md
│   │   ├── LibCookie.ts.md
│   │   └── LibNote.ts.md
│   ├── logo.svg
│   ├── react-app-env.d.ts.md
│   ├── reportWebVitals.ts.md
│   └── setupTests.ts.md
└── tsconfig.json.md

8 directories, 40 files


  • 検索できました。


まとめ

  • Obsidian検索が、なかなか速い。