UNPKG

9.41 kBMarkdownView Raw
1<h1 align="center">
2 @newswire/doc-to-archieml
3</h1>
4<p align="center">
5 <a href="https://circleci.com/gh/rdmurphy/doc-to-archieml"><img src="https://badgen.net/circleci/github/rdmurphy/doc-to-archieml/"></a>
6 <a href="https://www.npmjs.org/package/@newswire/doc-to-archieml"><img src="https://badgen.net/npm/v/@newswire/doc-to-archieml" alt="npm"></a>
7 <a href="https://david-dm.org/rdmurphy/doc-to-archieml"><img src="https://badgen.net/david/dep/rdmurphy/doc-to-archieml" alt="dependencies"></a>
8 <a href="https://packagephobia.now.sh/result?p=@newswire/doc-to-archieml"><img src="https://badgen.net/packagephobia/install/@newswire/doc-to-archieml" alt="install size"></a>
9</p>
10
11`@newswire/doc-to-archieml` is a simple wrapper around the [Google Docs API](https://developers.google.com/docs/api/) and [ArchieML](http://archieml.org) for converting the contents of a Google Doc into a ArchieML-produced data structure.
12
13## Key features
14
15- ⚙️ Produces identical output to **[ArchieML's example Google Drive export method](https://github.com/newsdev/archieml-js/tree/master#using-with-google-documents)** (including support for converting links to `<a>` tags) without the use of an HTML parser
16- 👩‍🔧 Does not expect any particular method of authenticating with Google — **use the authenticated Google API instance, Google Docs client or [authentication method](https://github.com/googleapis/google-api-nodejs-client#authentication-and-authorization) you are already using**
17
18## Installation
19
20`@newswire/doc-to-archieml` requires a version of Node.js **8 or higher**. It is available via `npm`.
21
22```sh
23npm install --save-dev @newswire/doc-to-archieml
24# or
25yarn add --dev @newswire/doc-to-archieml
26```
27
28## Table of contents
29
30<!-- START doctoc generated TOC please keep comment here to allow auto update -->
31<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
32
33
34- [Usage](#usage)
35- [Authentication](#authentication)
36 - [1) Passing authentication](#1-passing-authentication)
37 - [2) Passing an authenticated Google Docs API client](#2-passing-an-authenticated-google-docs-api-client)
38 - [3) Passing an authenticated Google APIs instance](#3-passing-an-authenticated-google-apis-instance)
39- [Contributing](#contributing)
40- [License](#license)
41
42<!-- END doctoc generated TOC please keep comment here to allow auto update -->
43
44## Usage
45
46`@newswire/doc-to-archieml` exports a single function - `docToArchieML`.
47
48```js
49const { docToArchieML } = require('@newswire/doc-to-archieml');
50const { google } = require('googleapis');
51
52async function main() {
53 // this method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS
54 // environment variables to establish authentication
55 const auth = await google.auth.getClient({
56 scopes: ['https://www.googleapis.com/auth/documents.readonly'],
57 });
58
59 // pass in the valid authentication and ID of the document you want to process
60 const results = await docToArchieML({ documentId: '...', auth });
61
62 console.log(results); // `results` is your ArchieML-produced JavaScript object
63}
64
65main().catch(console.error);
66```
67
68## Authentication
69
70`docToArchieML` has one required parameter — `documentId`. But the authentication you provide with the Google API may be handled in one of the three ways detailed below.
71
72_Acquiring_ this authentication is beyond the scope of this project's documentation, but two good starting points are [Google's official Node.js quickstart guide for the Google Docs API](https://developers.google.com/docs/api/quickstart/nodejs) and the [client library's authentication documentation](https://github.com/googleapis/google-api-nodejs-client#authentication-and-authorization).
73
74### 1) Passing authentication
75
76`docToArchieML` doesn't limit authentication to only OAuth2 (although it certainly supports it!) and will accept any authenticated client that the Google Docs API supports.
77
78After establishing authentication [using one of the methods](https://github.com/googleapis/google-api-nodejs-client#authentication-and-authorization) supported by `googleapis`, you can pass this auth directly to `docToArchieML` and it'll handle the rest.
79
80```js
81const { docToArchieML } = require('@newswire/doc-to-archieml');
82const { google } = require('googleapis');
83
84async function main() {
85 // this method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS
86 // environment variables to establish authentication
87 const auth = await google.auth.getClient({
88 scopes: ['https://www.googleapis.com/auth/documents.readonly'],
89 });
90
91 // pass in the valid authentication, which is used to create a Google Docs API client internally
92 const results = await docToArchieML({ documentId: '...', auth });
93}
94
95main().catch(console.error);
96```
97
98> (This example uses the [service-to-service authentication](https://github.com/googleapis/google-api-nodejs-client#service-to-service-authentication) method.)
99
100### 2) Passing an authenticated Google Docs API client
101
102Maybe you've been working with the Google Docs API and have already set up an authenticated instance of the Google Docs API client that has access to the docs you'd like to work with. `docToArchieML` will accept that and use it!
103
104```js
105const { docToArchieML } = require('@newswire/doc-to-archieml');
106const { google } = require('googleapis');
107
108async function main() {
109 // this method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS
110 // environment variables to establish authentication
111 const auth = await google.auth.getClient({
112 scopes: ['https://www.googleapis.com/auth/documents.readonly'],
113 });
114
115 // create your own Google Docs API client
116 const client = google.docs({
117 version: 'v1',
118 auth,
119 });
120
121 // pass in the authenticated Google Docs API client
122 const results = await docToArchieML({ documentId: '...', client });
123}
124
125main().catch(console.error);
126```
127
128> (This example uses the [service-to-service authentication](https://github.com/googleapis/google-api-nodejs-client#service-to-service-authentication) method.)
129
130### 3) Passing an authenticated Google APIs instance
131
132Maybe you've been using multiple Google API services and have [set authentication across all Google APIs globally](https://github.com/googleapis/google-api-nodejs-client#setting-global-or-service-level-auth). `docToArchieML` can accept the authenticated `googleApisInstance` and use that to create the Google Docs API client - no passing of `auth` necessary.
133
134```js
135const { docToArchieML } = require('@newswire/doc-to-archieml');
136const { google } = require('googleapis');
137
138async function main() {
139 // this method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS
140 // environment variables to establish authentication
141 const auth = await google.auth.getClient({
142 scopes: ['https://www.googleapis.com/auth/documents.readonly'],
143 });
144
145 // set auth as a global default
146 google.options({ auth });
147
148 // pass in the GoogleApisInstance, which will be used to connect to the Google Docs API
149 const results = await docToArchieML({ documentId: '...', google });
150}
151
152main().catch(console.error);
153```
154
155> (This example uses the [service-to-service authentication](https://github.com/googleapis/google-api-nodejs-client#service-to-service-authentication) method.)
156
157## Contributing
158
159First clone the repo to your local device and install the dependencies.
160
161```sh
162yarn
163```
164
165After making any changes, you'll need to run the tests. But this is a little tricky because we perform an integration test against live Google Doc files. To make the tests work for you locally, you'll need to do a few extra steps.
166
167First make a copy of the two test doc files:
168
169[Click here to make a copy of the basic test doc file](https://docs.google.com/document/d/1coln1etP5rT1MqmNtRT7lToGtCi1EAsDVzC5aq0LsIc/copy)
170[Click here to make a copy of the extensions test doc file](https://docs.google.com/document/d/1_v0gAswpNnGnDqAx7cU_1bFEK8J7fi8EBvfKvgGZubc/copy)
171
172Once you have both files, you'll need to get their IDs and set the correct environment variables so the test runner finds them. To get the IDs **look at the URLs of the files** in your browser - it is the long string of random characters and numbers near the end.
173
174https://<span></span>docs.google.com/document/d/**1coln1etP5rT1MqmNtRT7lToGtCi1EAsDVzC5aq0LsIc**/edit
175
176Set the following environmental variables in your shell:
177
178```sh
179export BASIC_DOCUMENT_ID=<basic_doc_id>
180export EXTENSIONS_DOCUMENT_ID=<extensions_doc_id>
181```
182
183Next you'll need to create a service account (or use an existing one) and give it access to your two copies of the docs. Typically this is done by sharing those files with the email of the service account in the document sharing interface.
184
185Finally, we need to tell the test runner how to use the service account authentication to communicate with the API. The best method for doing this is the [service-to-service authentication method](https://github.com/googleapis/google-api-nodejs-client#service-to-service-authentication). Typically this means setting the `GOOGLE_APPLICATION_CREDENTIALS` environmental variable and pointing it at the location of your service account authentication JSON file.
186
187```sh
188export GOOGLE_APPLICATION_CREDENTIALS=<path_to_json_file>
189```
190
191And... now you're ready to go! You should be able to run the tests.
192
193```sh
194yarn test
195```
196
197If anyone has any suggestions on how to make this a smoother process, please let me know!
198
199## License
200
201MIT