使用 Appscript 将 Markdown 文本文件转换为 Google 文档?

问题描述 投票:0回答:8

我正在尝试将大量用 Markdown 编写的文档迁移到 Google 文档中,以便我们的营销部门可以使用它。

是否有一种使用 appscript/Google Docs Api 的机制可以导入文件并使用预定义模板将其转换为 Google 文档? 例如 H1s 将映射到标题等

google-apps-script google-docs google-docs-api
8个回答
64
投票

对于许多开发人员来说,最简单的方法很可能不需要新工具(如果您愿意进行一些手动粘贴):

只需使用 Visual Studio Code。

  1. 将文本粘贴到编辑器中。
  2. 在右下角,选择 Markdown 作为语言。
  3. 右上角,单击预览按钮。
  4. 这将分割屏幕并显示渲染的 Markdown,您可以将其粘贴到谷歌文档中。
  5. 只要您没有数百个文档,此时保持粘贴降价/复制结果/重复就非常快了。

当然,如果有大量文档,您会需要比这更自动化的东西。


49
投票

一个建议:使用 Pandoc 将 Markdown 转换为 docx,然后使用 Google Drive API 导入到 Google Docs。

您还可以使用 Google Drive 网络界面来完成此操作:

  1. 使用 pandoc 将 markdown 转换为 ODT(或其他中间形式):
    pandoc MyFile.md -f markdown -t odt -s -o MyFile.odt
  2. 将 ODT 文件移至您的 Google 云端硬盘文件夹中。
  3. 右键单击 ODT 文件(在网络版 Drive 中),然后按“打开方式 -> Google 文档”。
  4. Google Drive 现在将创建一个新的 Google 文档,其内容与 ODT 文件匹配,并为您打开它。

30
投票

无需附加组件

我刚刚偶然发现了一种非常简单的方法,可能适合您的需求。

在 github 中,我打开了一个 ReadMe.md 文件,该文件在浏览器中呈现为富文本。我从浏览器复制它并将其粘贴到新的 Google 文档中。

Voila:Google 文档保留了标题、项目符号等。我不能保证它对链接和其他更高级的降价操作的作用,但这是一种快速入门的方法。


9
投票

使用 pandoc

建议的一种变体:尝试使用 
docx
格式而不是
odt
。 Google Docs 原生处理 MS Office 文件,因此我发现使用这种方法可以更好地保留格式。

修改步骤:

  1. 使用 pandoc 将 markdown 转换为 DOCX:
    pandoc MyFile.md -f markdown -t docx -s -o MyFile.docx
  2. 上传
    MyFile.docx
    到您的 Google 云端硬盘文件夹
  3. 在网页版 Drive 中双击
    MyFile.docx
  4. Google 云端硬盘将在新选项卡中打开
    MyFile.docx

3
投票

我不知道有什么工具或库可以直接从 Markdown 转换为 Google 文档。

也许您可以将 Markdown 转换为与 Google Docs 兼容的中间格式(可行的格式包括 .docx、.docm、.dot、.dotx、.dotm、.html、纯文本 (.txt)、.rtf 和 .odt)以及然后从那里开始。

您只需要找到一个可以将 Markdown 转换为其中一种格式并批量处理文件的工具(也许某些命令行实用程序可以帮助实现这一点)。


3
投票
#!/bin/bash

# Create the "converted" directory if it doesn't already exist
if [ ! -d "converted" ]; then
  mkdir "converted"
fi

# Find all markdown files in the current directory and its subdirectories
find . -name "*.md" | while read filename; do
  # Use pandoc to convert the file to a .docx file
  pandoc "$filename" -o "${filename%.*}.docx"

  # Create the same directory structure under "converted" as the original file
  dir=$(dirname "$filename")
  mkdir -p "converted/$dir"

  # Move the converted file to the "converted" directory
  mv "${filename%.*}.docx" "converted/$dir"
done

0
投票

有 Google Docs 插件 Markdown to Docs ... 可以将 Markdown 转换为 Google Docs。

由于 Gdoc 格式,它有其局限性,但在其他方面工作得很好。


0
投票

我为此创建了一个小脚本,现在位于 github 上。您几乎可以剪切和粘贴。我需要这个解决方案,但找不到任何解决方案,所以我使用谷歌应用程序脚本制作了它。 https://github.com/brandhorse/google-docs-markdown希望这对一些人有帮助。干杯。

© www.soinside.com 2019 - 2024. All rights reserved.