|
| 1 | +# Convert Google Docs, PDF, and Gmail to Markdown files |
| 2 | + |
| 3 | +The collection of scripts in this `apps_script` directory allows you to convert |
| 4 | +the contents of Google Drive folders and Gmail to Markdown files that are |
| 5 | +compatible with Docs Agent. |
| 6 | + |
| 7 | +The steps are: |
| 8 | + |
| 9 | +1. [Prepare a Google Drive folder](#1-prepare-a-google-driver-folder). |
| 10 | +2. [Mount Google Drive on your host machine](#2-mount-google-drive-on-your-host-machine). |
| 11 | +3. [Create an Apps Script project](#3-create-an-apps-script-project). |
| 12 | +4. [Edit and run main.gs on Apps Script](#4-edit-and-run-maings-on-apps-script). |
| 13 | +5. [Update config.yaml to include the mounted directory](#5-update-configyaml-to-include-the-mounted-directory). |
| 14 | + |
| 15 | +## 1. Prepare a Google Drive folder |
| 16 | + |
| 17 | +First, create a new folder in Google Drive and add your Google Docs (which will be |
| 18 | +used as source documents to Docs Agent) to the folder. |
| 19 | + |
| 20 | +Do the following: |
| 21 | + |
| 22 | +1. Browser to https://drive.google.com/. |
| 23 | +1. Click **+ New** on the top left corner. |
| 24 | +1. Click **New folder**. |
| 25 | +1. Name your new folder (for example, `my source Google Docs`). |
| 26 | +1. To enter the newly created folder, double click the folder. |
| 27 | +1. Add (or move) your source Google Docs to this new folder. |
| 28 | + |
| 29 | +## 2. Mount Google Drive on your host machine |
| 30 | + |
| 31 | +Mount your Google Drive to your host machine, so that it becomes easy to access the |
| 32 | +folders in Google Drive from your host machine (later in step 5). |
| 33 | + |
| 34 | +There are a variety of methods and tools available online that enable this setup |
| 35 | +(for example, see [`google-drive-ocamlfuse`][google-drive-ocamlfuse] for Linux machines). |
| 36 | + |
| 37 | +## 3. Create an Apps Script project |
| 38 | + |
| 39 | +Create a new Apps Script project and copy all the `.gs` scripts in this |
| 40 | +`apps_script` directory to your new Apps Script project. |
| 41 | + |
| 42 | +Do the following: |
| 43 | + |
| 44 | +1. Browse to https://script.google.com/. |
| 45 | +1. Click **New Project**. |
| 46 | +1. At the top of the page, click **Untitled Project** and enter a meaningful |
| 47 | + title (for example, `gDocs to Docs Agent`). |
| 48 | +1. Click the **+** icon next to **Files**. |
| 49 | +1. Click **Script**. |
| 50 | +1. Name the new script to be one of the `.gs` files in this `apps_script` directory |
| 51 | + (for example, `drive_to_markdown`). |
| 52 | +1. Copy the content of the `.gs` file to the new script on your Apps Script project. |
| 53 | +1. To save, click the "Save project" icon in the toolbar. |
| 54 | +1. Repeat the steps until all the `.gs` files are copied to your Apps Script project. |
| 55 | +1. Click the **+** icon next to **Services**. |
| 56 | +1. Scroll down and click **Drive API**. |
| 57 | +1. Click **Add**. |
| 58 | + |
| 59 | +You are now ready to edit the parameters on the `main.gs` file to select a folder |
| 60 | +in Google Drive and export emails from Gmail. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +**Figure 1**. A screenshot of an example Apps Script project. |
| 65 | + |
| 66 | +## 4. Edit and run main.gs on Apps Script |
| 67 | + |
| 68 | +Edit the `main.gs` file on your Apps Script project to select which functions |
| 69 | +(features) you want to run. |
| 70 | + |
| 71 | +Do the following: |
| 72 | + |
| 73 | +1. Browse to your project on https://script.google.com/. |
| 74 | + |
| 75 | +1. Open the `main.gs` file. |
| 76 | + |
| 77 | +1. In the `main` function, comment out any functions that you don't want to run |
| 78 | + (see Figure 1): |
| 79 | + |
| 80 | + * `convertDriveFolderToMDForDocsAgent(folderInput)`: This function converts |
| 81 | + the contents of a Google Drive folder to Markdown files (currently only Google |
| 82 | + Docs and PDF). Make sure to specify a valid Google Drive folder in the `folderInput` |
| 83 | + variable. Use the name of the folder created in **step 1** above, for example: |
| 84 | + |
| 85 | + ``` |
| 86 | + var folderInput = "my source Google Docs" |
| 87 | + function main() { |
| 88 | + convertDriveFolderToMDForDocsAgent(folderInput); |
| 89 | + //exportEmailsToMarkdown(SEARCH_QUERY, folderOutput); |
| 90 | + } |
| 91 | + ``` |
| 92 | +
|
| 93 | + * `exportEmailsToMarkdown(SEARCH_QUERY, folderOutput)`: This function converts |
| 94 | + the emails returned from a Gmail search query into Markdown files. Make sure to |
| 95 | + specify a search query in the `SEARCH_QUERY` variable. You can test this search |
| 96 | + query directly in the Gmail search bar. Also, specify an output directory for the |
| 97 | + resulting emails. |
| 98 | +
|
| 99 | +1. To save, click the "Save project" icon in the toolbar. |
| 100 | +
|
| 101 | +1. Click the "Run" icon in the toolbar. |
| 102 | +
|
| 103 | + When this script runs successfully, the Execution log panel prints output similar |
| 104 | + to the following: |
| 105 | +
|
| 106 | + ``` |
| 107 | + 9:55:59 PM Notice Execution completed |
| 108 | + ``` |
| 109 | +
|
| 110 | + Also, the script creates a new folder in your Google Drive and stores the converted |
| 111 | + Markdown files in this folder. The name of this new folder has `-output` as a postfix. |
| 112 | + For example, with the folder name `my source Google Docs`, the name of the new folder |
| 113 | + is `my source Google Docs-output`. |
| 114 | +
|
| 115 | + With Google Drive mounted on your host machine in step 2, you can now directly access |
| 116 | + this folder from the host machine, for example: |
| 117 | +
|
| 118 | + ``` |
| 119 | + user@hostname:~/DriveFileStream/My Drive/my source Google Docs-output$ ls |
| 120 | + Copy_of_My_Google_Docs_To_Be_Converted.md |
| 121 | + ``` |
| 122 | +
|
| 123 | +## 5. Update config.yaml to include the mounted directory |
| 124 | +
|
| 125 | +Once you have your Google Drive mounted on the host machine, you can now |
| 126 | +specify one of its folders as an input source directory for Docs Agent. |
| 127 | +
|
| 128 | +Do the following: |
| 129 | +
|
| 130 | +1. In the Docs Agent project, open the [`config.yaml`][config-yaml] file |
| 131 | + with a text editor. |
| 132 | +
|
| 133 | +1. Specify your mounted Google Drive folder as an `input` group, for example: |
| 134 | +
|
| 135 | + ``` |
| 136 | + input: |
| 137 | + - path: "/home/user/DriveFileStream/My Drive/my source Google Docs-output" |
| 138 | + url_prefix: "docs.google.com" |
| 139 | + ``` |
| 140 | +
|
| 141 | + You **must** specify a value to the `url_prefix` field, such as `docs.google.com`. |
| 142 | + Currently this value is used to generate hashes for the content. |
| 143 | +
|
| 144 | +1. (**Optional**) Add an additional Google Drive folder for your exported emails, |
| 145 | + for example: |
| 146 | +
|
| 147 | + ``` |
| 148 | + input: |
| 149 | + - path: "/home/user/DriveFileStream/My Drive/my source Google Docs-output" |
| 150 | + url_prefix: "docs.google.com" |
| 151 | + - path: "/home/user/DriveFileStream/My Drive/psa-output" |
| 152 | + url_prefix: "mail.google.com" |
| 153 | + ``` |
| 154 | +
|
| 155 | +1. Save the changes in the `config.yaml` file. |
| 156 | +
|
| 157 | +You're all set with a new documentation source for Docs Agent. You can now follow the |
| 158 | +instructions in the project's main [`README`][main-readme] file to launch the Docs Agent app. |
| 159 | +
|
| 160 | +<!-- Reference links --> |
| 161 | +
|
| 162 | +[config-yaml]: ../config.yaml |
| 163 | +[main-readme]: ../README.md |
| 164 | +[google-drive-ocamlfuse]: https://github.com/astrada/google-drive-ocamlfuse |
0 commit comments