1+ const obsidian = require ( 'obsidian' ) ;
2+
3+ module . exports = class FolderContextMenuPlugin extends obsidian . Plugin {
4+ async onload ( ) {
5+ // Register the command
6+ this . addCommand ( {
7+ id : 'copy-folder-contents' ,
8+ name : 'Copy Folder Contents' ,
9+ checkCallback : ( checking ) => {
10+ const activeFile = this . app . workspace . getActiveFile ( ) ;
11+ const activeFolder = activeFile ? activeFile . parent : null ;
12+
13+ if ( activeFolder ) {
14+ if ( ! checking ) {
15+ this . copyFolderContents ( activeFolder ) ;
16+ }
17+ return true ;
18+ }
19+ return false ;
20+ }
21+ } ) ;
22+
23+ // Add the context menu item
24+ this . registerEvent (
25+ this . app . workspace . on ( 'file-menu' , ( menu , file ) => {
26+ if ( file instanceof obsidian . TFolder ) {
27+ menu . addItem ( ( item ) => {
28+ item
29+ . setTitle ( 'Copy contents' )
30+ . setIcon ( 'files' )
31+ . onClick ( ( ) => this . copyFolderContents ( file ) ) ;
32+ } ) ;
33+ }
34+ } )
35+ ) ;
36+ }
37+
38+ async copyFolderContents ( folder ) {
39+ const content = await this . recursiveCopyMdFiles ( folder ) ;
40+ await navigator . clipboard . writeText ( content ) ;
41+ new obsidian . Notice ( `Copied Markdown from ${ folder . name } to clipboard` ) ;
42+ }
43+
44+ async recursiveCopyMdFiles ( folder ) {
45+ let content = '' ;
46+ for ( const child of folder . children ) {
47+ if ( child instanceof obsidian . TFile && child . extension === 'md' ) {
48+ content += `File: ${ child . path } \n\n` ;
49+ content += await this . app . vault . read ( child ) ;
50+ content += '\n\n---\n\n' ;
51+ } else if ( child instanceof obsidian . TFolder ) {
52+ content += await this . recursiveCopyMdFiles ( child ) ;
53+ }
54+ }
55+ return content ;
56+ }
57+ }
0 commit comments