开发社区您好!
今天我将为您提供一个如何将 dev.to 文章转换为 Docusaurus 博客的编程示例。此示例将使用 Node.js 从 dev.to API 获取文章并创建与 Docusaurus 博客文章兼容的 Markdown 文件。
首先,让我们设置项目并安装必要的依赖项:
mkdir devto-to-docusaurus cd devto-to-docusaurus npm init -y npm install axios fs-extra front-matter
现在,创建一个名为convert.js 的文件并添加以下代码:
const axios = require('axios'); const fs = require('fs-extra'); const frontMatter = require('front-matter'); const path = require('path'); const DEV_TO_API_KEY = 'YOUR_DEV_TO_API_KEY'; const DEV_TO_USERNAME = 'YOUR_DEV_TO_USERNAME'; const OUTPUT_DIR = './blog'; async function fetchDevToArticles() { const response = await axios.get(`https://dev.to/api/articles/me/published`, { headers: { 'api-key': DEV_TO_API_KEY } }); return response.data; } function convertToDocusaurusFrontMatter(article) { const date = new Date(article.published_at); return { title: article.title, authors: [DEV_TO_USERNAME], tags: article.tag_list, date: date.toISOString().split('T')[0], slug: article.slug, description: article.description, }; } async function convertArticleToMarkdown(article) { const frontMatterContent = convertToDocusaurusFrontMatter(article); const markdown = `--- ${Object.entries(frontMatterContent).map(([key, value]) => `${key}: ${Array.isArray(value) ? `[${value.join(', ')}]` : value}`).join('\n')} --- ${article.body_markdown}`; const fileName = `${frontMatterContent.date}-${article.slug}.md`; const filePath = path.join(OUTPUT_DIR, fileName); await fs.outputFile(filePath, markdown); console.log(`Created: ${filePath}`); } async function main() { try { const articles = await fetchDevToArticles(); await fs.ensureDir(OUTPUT_DIR); for (const article of articles) { await convertArticleToMarkdown(article); } console.log('Conversion completed successfully!'); } catch (error) { console.error('Error:', error.message); } } main();
此脚本的工作原理如下:
要使用此脚本:
运行脚本后,您将在博客目录[1][3]中发现您的 dev.to 文章已转换为与 Docusaurus 兼容的博客文章。
要将这些帖子集成到您的 Docusaurus 网站中:
以下是 docusaurus.config.js 的示例:
module.exports = { // ... other config options presets: [ [ '@docusaurus/preset-classic', { blog: { path: 'blog', routeBasePath: 'blog', blogTitle: 'My Dev.to Blog', blogDescription: 'A collection of my dev.to articles', blogSidebarCount: 'ALL', blogSidebarTitle: 'All Blog Posts', showReadingTime: true, editUrl: 'https://github.com/yourusername/your-repo/edit/main/website/', }, }, ], ], // ... other config options };
此配置将博客插件设置为使用博客目录并在侧边栏中显示所有帖子[1][3]。
请记住根据您的项目结构和偏好自定义 editUrl 和其他选项。通过此设置,您的 dev.to 文章将无缝集成到您的 Docusaurus 博客中。
引用:
[1] https://docusaurus.io/docs/blog
[2] https://dev.to/dreamlogic/how-to-use-the-multi-blog-plugin-for-docusaurus-32a2
[3] https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-content-blog
[4] https://dev.to/tuenguyen2911_67/inspired-by-docusaurus-2cm6
[5] https://dev.to/dianaiminza/documentation-using-docusaurus-40g5
[6] https://dev.to/seanyasno/starting-a-new-blogging-journey-with-docusaurus-3aap
[7] https://dev.to/lo_victoria2666/build-beautiful-documentation-websites-with-docusaurus-8o2
[8] https://dev.to/katiel/exploring-the-code-behind-docusaurus-pbk
以上是Dev.to 发布到 Docusaurus 博客(自动化 Nodejs)的详细内容。更多信息请关注PHP中文网其他相关文章!