项目作者: AnandChowdhary

项目描述 :
📝 Convert a Google Docs file (API response) to Markdown
高级语言: TypeScript
项目地址: git://github.com/AnandChowdhary/docs-markdown.git
创建时间: 2020-04-16T19:08:22Z
项目社区:https://github.com/AnandChowdhary/docs-markdown

开源协议:MIT License

下载


📝 Docs Markdown

Convert a Google Docs file (API response) to Markdown

Node CI
Travis CI
Coverage
Dependencies
License
Vulnerabilities
Based on Node.ts
npm type definitions
npm package
npm downloads
Contributors
semantic-release

npm

💡 Usage

Install the package from npm:

  1. npm install docs-markdown

API

The fetchGoogleDocsFiles helper can download a document from Google Docs and save it as a markdown file:

  1. import { fetchGoogleDocsFiles } from "docs-markdown";
  2. // Google Docs document ID
  3. await fetchGoogleDocsFiles(["1UEUrJ98RXu9BNcFj3pMgiUALQpjIb8Y-gNu-YhlYvFQ"]);
  4. // Google Docs document ID and file name
  5. await fetchGoogleDocsFiles(["1UEUrJ98RXu9BNcFj3pMgiUALQpjIb8Y-gNu-YhlYvFQ:filename.md"]);
  6. // Multiple Google Docs documents (comma-separated string)
  7. await fetchGoogleDocsFiles(["documentId1", "documentId2", "documentId3"]);

You can also use the googleDocsToMarkdown function to manually convert documents:

  1. import { googleDocsToMarkdown } from "docs-markdown";
  2. import { google } from "googleapis";
  3. import { writeFileSync } from "fs";
  4. const oauth2Client = new google.auth.OAuth2(); // Authenticate
  5. const docs = google.docs("v1");
  6. const file = await docs.documents.get({
  7. documentId: "Google Docs document ID",
  8. auth: oauth2Client,
  9. });
  10. const markdown = googleDocsToMarkdown(file.data);
  11. writeFileSync("file.md", markdown);

CLI

Fetch files and save them as markdown:

  1. # Google Docs document ID
  2. docs-markdown fetch "1UEUrJ98RXu9BNcFj3pMgiUALQpjIb8Y-gNu-YhlYvFQ"
  3. # Google Docs document ID with file name
  4. docs-markdown fetch "1UEUrJ98RXu9BNcFj3pMgiUALQpjIb8Y-gNu-YhlYvFQ:filename.md"
  5. # Multiple Google Docs documents
  6. docs-markdown fetch "documentId1, documentId2, documentId3"
  7. # Convert a JSON document to markdown
  8. docs-markdown convert "path/to/file.json"

Authentication

The following environment variables are required to fetch files from Google Docs. They are not required when converting JSON documents to markdown:

  • GOOGLE_DOCS_CLIENT_ID
  • GOOGLE_DOCS_CLIENT_SECRET
  • GOOGLE_DOCS_ACCESS
  • GOOGLE_DOCS_REFRESH

To learn how to create a client ID and secret, read the article Using OAuth 2.0 to Access Google APIs. Once you’ve created them, create an access token and a refresh token, use the OAuth 2.0 Playground with your client ID and secret.

⭐️ Features

  • Paragraphs
  • Headings, titles, subtitles
  • Bold, italic
  • Lists
  • Links
  • Images
  • Tables
  • Header, footer

🍳 Recipes

GitHub Actions + Google Docs CI

If you want to sync your Google Docs documents as markdown files to a GitHub repository, you can use this GitHub Actions workflow that runs every day, fetches your documents, and commit them to your repo. Make sure you have all the required environment variables stored as GitHub Secrets:

  1. name: Google Docs
  2. on:
  3. schedule:
  4. - cron: "0 0 * * *"
  5. jobs:
  6. release:
  7. name: Fetch Google Docs
  8. runs-on: ubuntu-18.04
  9. steps:
  10. - name: Checkout
  11. uses: actions/checkout@v2
  12. - name: Setup Node.js
  13. uses: actions/setup-node@v1
  14. with:
  15. node-version: 12
  16. - name: Download files
  17. run: npx docs-markdown fetch "1UEUrJ98RXu9BNcFj3pMgiUALQpjIb8Y-gNu-YhlYvFQ"
  18. env:
  19. GOOGLE_DOCS_ACCESS: ${{ secrets.GOOGLE_DOCS_ACCESS }}
  20. GOOGLE_DOCS_REFRESH: ${{ secrets.GOOGLE_DOCS_REFRESH }}
  21. GOOGLE_DOCS_CLIENT_ID: ${{ secrets.GOOGLE_DOCS_CLIENT_ID }}
  22. GOOGLE_DOCS_CLIENT_SECRET: ${{ secrets.GOOGLE_DOCS_CLIENT_SECRET }}
  23. - name: Commit new data
  24. uses: stefanzweifel/git-auto-commit-action@v4.1.1
  25. with:
  26. commit_message: "Update Google Docs file"
  27. commit_user_name: GitHub Actions
  28. commit_user_email: actions@github.com
  29. commit_author: GitHub Actions <actions@github.com>

👩‍💻 Development

Build TypeScript:

  1. npm run build

📄 License

MIT © Anand Chowdhary