Skip to content

Instantly share code, notes, and snippets.

@igrep
Last active September 20, 2025 09:31
Show Gist options
  • Save igrep/3618bcdf0294c81bda3481464be96bc8 to your computer and use it in GitHub Desktop.
Save igrep/3618bcdf0294c81bda3481464be96bc8 to your computer and use it in GitHub Desktop.
MDNの翻訳を始めるときに便利なNeovim向け関数群(Vimへの移植もそんなに難しくないはず)
" # 使用方法
" :call BeginTranslation()
" :call OpenJaPath()
"
" # 概要
"
" ## BeginTranslation
" MDNの翻訳に取りかかる際、現在開いている英語版のパスを日本語版のパスに変換して英語版をコピーし、開く
" その後Front-matterを編集し、title と slug 以外を削除し、l10n を追加することで、翻訳の準備を整える
"
" ## OpenJaPath
" 現在開いている英語版のパスを日本語版のパスに変換して開く
"
" # 使用時の前提
" - WindowsのNeovimで実行する
" - translated-contentとcontentが同じディレクトリーにあり、フルパスに空白文字などcmd.execのメタキャラクターを含まない
" - カレントディレクトリーがその、translated-content・contentがあるディレクトリーである
" - (部分的な再利用がしやすいよう)グローバルに各種関数を追加するため、名前が衝突しないこと
"
" # 参考
" https://mozilla-japan.github.io/mdn-translation-guide/translation/5_new-ja-file.html
" https://mozilla-japan.github.io/mdn-translation-guide/translation/6_edit-ja-file.html
function! ToJaPath(enPath) abort
" e.g. C:\path\to\content\files\en-us\glossary\boolean\html\index.md
return substitute(a:enPath, '\\content\\files\\en-us', '\\translated-content\\files\\ja', '')
endfunction
function! PrepareJaPath() abort
let path_to_index_md = ToJaPath(expand('%:p'))
call mkdir(v:lua.vim.fs.dirname(path_to_index_md), "p")
return path_to_index_md
endfunction
function! OpenJaPath() abort
execute "edit " . PrepareJaPath()
endfunction
function! CopyToJaPath() abort
let path_to_index_md = PrepareJaPath()
execute "w " . path_to_index_md
execute "edit " . path_to_index_md
endfunction
function! BeginMdnTranslation() abort
cd content
let source_commit = trim(system('git log -n1 --pretty="format:%H" -- ' . expand('%:p')))
cd ..
call CopyToJaPath()
call InitializeFrontMatter(source_commit)
endfunction
function! InitializeFrontMatter(source_commit) abort
" 先頭の行から「---」までを取得
1
silent /\v^---$/
let front_matter_end_line = line('.') - 1
" 先頭の行より次から title: と slug: の行以外を削除
execute 'silent! 2,' . front_matter_end_line . 'v/\v^(title:|slug:)/d'
" 再び「---」に移動し、該当の行より上に l18n を追加
1
silent /\v^---$/
call append(line(".") - 1, ["l10n:", " sourceCommit: " . a:source_commit])
w
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment