>您可以轻松地将您的想法发布到诸如Dev.to,Hashnode或Medium之类的网站,但理想是对自己的内容充分控制。有一个不断增长的工具清单,用于构建自己的网站并控制自己的内容。在这个广泛的教程中,我将介绍如何使用盖茨比(Gatsby)发出内容,并在这种生态系统中获得添加的铃铛和哨子。
>>我最初使用jekyll发布我的博客,但随后使用Lumen模板切换到Gatsby。自2017年5月左右,我一直在使用Gatsby。
>>我将从你好,世界上去! Gatsby项目通过代码语法突出显示的编码博客和该深色模式的主题切换。
>有一个丰富的插件,开头和主题生态系统可供盖茨比迅速启动并运行,但我想采用一种渐进的披露方法来展示盖茨比,重点是盖茨比项目的基础知识。 >
为什么盖茨比?Markdown和MDX
>自2016年以来,我一直在记录自己的开发旅程。 Markdown提供了一种启用可以转换为HTML的纯文本文件中简单编辑的方法。
到目前为止,Gatsby是我用于使用Markdown和MDX的最佳框架,因为上面不需要使用FrontMatter的特殊符号。 如果您要遵循,则需要一些事情:
如果您没有这些,则既有stackblitz and github代码码
酷。现在,在其他任何地方去其他任何地方之前,我都需要在安装任何NPM模块之前添加.gitignore文件: >现在,我可以安装所有需要的NPM优点,而无需VS代码git尖叫我太多的活动更改。现在,让我们安装一些依赖项,以与Gatsby一起启动并运行: 接下来,我们将为该项目添加第一个React组件(许多)。我将以下内容添加到我创建的index.js文件中:
>直接使用命令行接口(CLI)的gatsby二进制命令是完全可行的,但是大多数人会将可用命令添加到package.json文件上的脚本部分。
作为额外的奖励,可以在此处添加一些其他功能。 如果我们想打开“浏览器”选项卡,一旦项目准备就绪,我们可以将-O添加到脚本中。 我将使用服务脚本做同样的事情,所以我知道当我构建一个项目时,它是在开发一个端口上的不同端口: ,强制性的“你好,世界!”欢迎使用,我可以继续介绍本文的其余部分! ? 最后,我将提交我到目前为止所做的更改: >博客的内容 好吧,该项目现在没有很多事情要做,所以首先,我将再次从命令行中添加一些内容: >您会注意到文件扩展名.mdx。这是一个MDX文件。
前提是一种存储有关盖茨比在构建页面时可以使用的文件的信息的方法。现在,我将添加帖子标题和日期。我还将为他们添加一些内容。这是我们的第一篇文章:
第三篇文章: >目前的帖子就是这样,因为这些帖子尚未被盖茨比(Gatsby)识别为页面。我需要让Gatsby知道在哪里可以找到可以添加到项目中的内容。为此,我将向盖茨比添加一个配置文件。 gatsby配置是用于定义和配置您可以使用的许多盖茨比插件的方法。在盖茨比插件生态系统上有更多信息。现在,我将在终端中再次创建文件: 盖茨比插件
>要注意的一件事是,在盖茨比(Gatsby)中,不需要在您的组件中导入react 17的反应。但是,为了完整,为了避免任何混乱,我将其包括在这些示例中。
>
commit变化到现在: gatsby graphql
graphiql查询面板和结果旁边。在这里,我将编写GraphQl查询以检索所需的数据。查询面板底部还有一个查询变量部分,稍后我将进行。 是GraphQl文档资源管理器。由于GraphQl的严格键入,这意味着它能够在其数据上生成自己的文档。但这超出了这篇文章的范围。 >与GraphQl 接下来,我将查询我在Graphiql查询面板中之前添加的文件。在此查询中,我要查询文件字体问题中定义的标题和日期:
这是一个很大的JSON对象,其中包含我们在查询中要求的相关信息。我们将尽快查看如何使用它。目前,这意味着我们可以在Gatsby项目中使用此数据来制作页面。
i
然后,将SiteMetAdata对象添加到Gatsby配置文件:
>,然后开发纱线),然后在GraphIQL Explorer中刷新页面并再次运行查询以获取数据: 做一个站点元数据钩
>关于其中一些符号的快速注释:const {stity:{siteMetAdata},}是快速获取网站查询中数据的方法,在此我将SiteMetAdata从站点对象中拉出。这被称为破坏性。>
现在,在我再次启动Dev Server之后,我可以转到Windows/Linux中的浏览器控制台( j在macOS上),请参见控制台输出中的sitemetadata对象。>
我将获得以下控制台输出:
>不必担心找不到缺少404页的控制台警告(net :: err_aborted 404(找不到))。我稍后再做。>
要避免每次都必须编写此查询,我想在组件中使用它。我将把它抽到自己的钩子中:
>现在,我将在新创建的src/hooks/use-supe-metadata.js文件中添加钩子,以获取site get ecter ot-octe of the网站元数据: 您可能已经注意到,此查询与Graphiql Explorer的查询不同: 这是为了命名查询。因为我会在项目中使用很多查询,所以给他们有意义的名字很有意义。
是时候到目前为止进行更改了: >主题UI
>
在src/gatsby-plugin-theme-ui/index.js文件中,我将添加一些主题UI预设,定义主题对象,然后在瑞士预设中传播到主题,主题颜色和样式。 >对于黑暗模式,我使用的是深层主题UI预设,并将其扩散到模式对象中的黑暗对象。 (很快就会详细介绍。
布局组件 >
>而不是让标题组件中使用的使用intemetadata,而是将我需要从布局组件的标头传递给标头,这是标题将要使用的位置。 (不久此信息。)首先,这是居住在src/components/header.js的标题组件: >我使用主题UI布局元素添加了一些基本样式。这看起来与以前有些不同:框,链接,标题……什么?这些都是所有主题UI组件,可用于布局,形式元素等。 >您可能会注意到AS = {gatsbylink}链接prop添加到链接组件中。这在主题UI中使用AS Prop,并让将组件传递到主题UI样式中。
Paul Scanlon有一篇很棒的帖子,更详细地解释了如何在主题UI中完成。为了对主题UI进行真正的全面解释,同一作者也有“理解主题UI”。
现在,位于src/components/layout.js中的布局组件: >
>
结果是标题,在布局组件中提供,并包装H1。
>现在是时候获取我在开始时创建的帖子,并在索引页面上显示它们作为可点击链接的列表。 >获取帖子信息,我将重新创建有关使用GraphQl查询本地文件的部分中的查询
> >将其添加到Graphiql Explorer中给了我这个结果:
>
>此方法具有针对页面的特殊文件符号,当Gatsby在构建时间生成文件系统数据时,将针对目标。该文件指示节点和sl。我将首先创建文件,然后详细说明数据来自何处:
如果我将查询粘贴到我的graphiql explorer中,然后按播放按钮,我会得到:
在此文件中,您可能会注意到我已经从返回的数据中破坏了尸体,然后从前桅杆上,在两级破坏中从前桅杆上产生标题: 的另一种方法是: 使用破坏使它的详细信息要少得多。 >要注意的最后一件事是将帖子的主体包裹起来。这是前物质块之后的.mdx文件中包含的所有内容。从GraphIQL查询中进行了评论的MDX,这是在MDXRenderer中包装的内容: 我现在将提交更改: 现在,单击索引页面上的一个链接将带我到所需的.mdx页面,但看起来与索引页面有些不同,对吗?
>
>
然后,在gatsby-browser.js和gatsby-ssr.js文件中,我将添加此信息: 如果包装功能需要任何更改,我可以在一个文件root-wrapper.js。
>由于在此处使用布局组件来包装网站上的所有页面元素,因此无需将其保存在索引页面上,因此我将从src/peg/pages/index.js中删除该页面:
>
>
>我需要安装软件包并在称为组件的盖茨比 - plugin-theme-ui文件夹中创建一个组件。
提交更改并移至下一部分:
这是可选的,我不会详细介绍这里发生的事情。只知道对悬停而产生的CSS效果很不错。 >创建了该组件,我可以将其导入我要使用的任何.mdx文件。在此示例中,我将其添加到Content/2021/03/third-post/index.mdx中。现在,我添加了该组件: 再次启动开发服务器后,我可以转到添加该组件的帖子,当我将悬停在 >您可能会对该导入进行恶作剧:../../../。不断!但是,有一种方法可以解决这个问题,但是使用我之前详细介绍的根包装器概念,并使用MDXProvider,它将 - 啊! - 向MDX提供您传递给它的任何组件。 >
现在,我可以从.mdx文件中删除导入: >停止并重新启动开发服务器后,我可以转到这篇文章,仍然看到RainbowText工作。直接将组件直接添加到MDXProvider的额外优势在于,当您要使用时,无需将组件导入.mdx文档。它可通过提供商用于所有MDX文档。
./mdx-logo.png是我添加到content/2021/03/06/hello-world文件夹中的文件,我将其称为相对文件。不过,这不是。如果我转到“ Hello World帖子”,则显示的图像被打破了。我需要将gatsby-remark-images作为插件添加到gatsby-plugin-mdx上,以便知道该如何处理图像文件:
>
该组件需要很多道具,其中许多是在整个站点中定义的一次,因此添加这些的最佳位置是在Sitemetadata对象中。然后,我可以用usedemetadata钩子拉出所需的东西。
>现在,这些其他属性就在SiteMetadata对象上了,我需要能够查询它们。当前,使用initemetadata Hook仅具有标题和描述,因此我现在将其余的内容添加: >我将SEO组件添加到所有页面。首先,我将在src/pages/{mdx.slug} .js page中完成帖子页面。这是最参与其中之一,因此我将在此处丢弃差异,并详细说明发生了什么: siteurl,slug和摘录是规范链接(在SEO中非常重要),摘录是用于元描述的。
>
>很多。我希望它的某些有意义!对于这篇文章的范围,我将其留在那里,但是还有很多关于这个主题的知识,我的意思是!
幸运的是,SRC/pages/index.js页面更简单!>
ctrl u 查看页面源。从这里,我可以检查典型的元标记,这将是元数据中使用的虚拟URL。
好吧!将此付诸实践并继续前进:
>在存储库名称中,我将添加项目名称my-gatsby-blog,但留下其余的默认设置,然后单击“创建存储库”。 下一个屏幕为我提供了我需要将本地项目推向github的终端命令:
Enter
是时候把这个婴儿放在网上了!有很多方法可以做到这一点。因为盖茨比(Gatsby)构建到平坦的文件结构,因此您可以在任何文件服务器上托管一个具有访问Internet的文件服务器。
要与Vercel部署,您需要一个GitHub,Gitlab或Bitbucket帐户进行身份验证。然后,您将提示您安装Vercel CLI:
的所有问题回答默认值: >在Vercel仪表板上,我可以配置该域,也可以从Vercel购买一个。我个人使用namecheap.com,但这是一种选择。 >
>
NETLIFY将构建文件并将其部署到生成的URL进行检查。与Vercel几乎相同,Netlify将使您在那里购买一个域并部署到它。
>
>名称:my-gatsby-blog
>可选的盖茨比插件
您是否想嵌入YouTube视频,推文,Strava运行,Coodepens和Codesandbox?查看gatsby-plugin-mdx-embed。
>要在盖茨比网站上实现fathom Analytics,我需要在我的网站的头部添加附加脚本标签。这意味着什么?好吧,首先,我需要在我的fathom仪表板上创建网站,然后访问https://app.usefathom.com/#/settings/sites/sites,滚动到列表的底部,添加我的新网站( my-gatsby-blog),然后单击“获取站点代码”。然后,我获得了带网站代码的弹出模式。我需要将其添加到Gatsby Project的负责人的脚本中。这是脚本的样子: 这是root-wrapper.js的差异: 就是我的。非常感谢您到最后。 ? 我希望您能从该广泛的指南中获得有关从头开始建立盖茨比项目的信息! 如果您想伸出手打个招呼,那么最好的地方是Twitter。
>如何自定义我的Gatsby MDX博客的布局? > Gatsby MDX允许您自定义博客文章的布局。您可以在“ SRC”目录中创建一个布局组件。该组件可以包括标头,页脚或侧边栏等元素。创建了布局组件后,您可以将MDX内容包裹起来。为此,请在您的MDX文件中导入布局组件,然后将您的内容包装在此处: gatsby mdx支持在您的博客文章中包含代码片段。要添加代码段,您可以使用“ PRE”和“代码”标签。用这样的标签包装代码片段: 我如何在我的盖茨比MDX博客中添加分页? >如何将社交共享按钮添加到我的Gatsby MDX博客文章中? >如何将新闻通讯订阅表格添加到我的gatsby MDX博客? >我需要什么?
>
<span>import <span>{ RainbowText }</span> from './components/rainbow';
</span>## <span>A Markdown Heading
</span><span><span><span><RainbowText</span>></span>Wheeeeeeee<span><span></RainbowText</span>></span>
</span>
><span>import <span>{ RainbowText }</span> from './components/rainbow';
</span>## <span>A Markdown Heading
</span><span><span><span><RainbowText</span>></span>Wheeeeeeee<span><span></RainbowText</span>></span>
</span>
<span># create the project directory
</span><span>mkdir my-gatsby-blog
</span><span># change into the directory
</span><span>cd my-gatsby-blog
</span><span># initialise a package.json file
</span><span>yarn init -y
</span><span># initialise the git repo
</span><span>git init
</span>
<span># create .gitignore file in my directory
</span><span>touch .gitignore
</span><span># add ignore contents with echo
</span><span>echo "# Project dependencies
</span><span>.cache
</span><span>node_modules
</span><span>
</span><span># Build directory
</span><span>public
</span><span>
</span><span># Other
</span><span>.DS_Store
</span><span>yarn-error.log" > .gitignore
</span>
<span>yarn add gatsby react react-dom
</span><span># -p is to create parent directories too if needed
</span><span>mkdir -p src/pages
</span><span># create the index (home) page
</span><span>touch src/pages/index.js
</span>
<span>import <span>React</span> from "react";
</span>
<span>export default function <span>IndexPage</span>() {
</span> <span>return <span><span><h1</span>></span>Hello, World!<span><span></h1</span>></span>;
</span><span>}
</span>
<span># if you're using npm ?
</span><span># $(npm bin)/gatsby develop
</span><span>yarn gatsby develop
</span>
<span>"scripts": {
</span> <span>"build": "gatsby build",
</span> <span>"dev": "gatsby develop",
</span> <span>"serve": "gatsby serve",
</span> <span>"clean": "gatsby clean"
</span><span>},
</span>
<span>"scripts": {
</span> <span>"build": "gatsby build",
</span> <span>"dev": "gatsby develop -p 8945 -o",
</span> <span>"serve": "gatsby serve -p 9854 -o",
</span> <span>"clean": "gatsby clean"
</span><span>},
</span>
我将在我制作的整个示例中使用这些。
<span># add everything for committing
</span><span>git add .
</span><span># commit to repo
</span><span>git commit -m 'init project'
</span>
这是我们的第二篇文章:<span># this creates the folders in the root of the project
</span><span>mkdir -p content/2021/03/{06/hello-world,07/second-post,08/third-post}
</span><span># create individual files
</span><span>touch content/2021/03/06/hello-world/index.mdx
</span><span>touch content/2021/03/07/second-post/index.mdx
</span><span>touch content/2021/03/08/third-post/index.mdx
</span>
<span><span>---
</span></span><span><span><span>title: Hello World - from mdx!
</span></span></span><span><span><span>date: 2021-03-06</span>
</span></span><span><span>---</span>
</span>
My first post!!
<span><span>## h2 Heading</span>
</span>
Some meaningful prose
<span><span>### h3 Heading</span>
</span>
Some other meaningful prose
<span>import <span>{ RainbowText }</span> from './components/rainbow';
</span>## <span>A Markdown Heading
</span><span><span><span><RainbowText</span>></span>Wheeeeeeee<span><span></RainbowText</span>></span>
</span>
gatsby config
<span># create the project directory
</span><span>mkdir my-gatsby-blog
</span><span># change into the directory
</span><span>cd my-gatsby-blog
</span><span># initialise a package.json file
</span><span>yarn init -y
</span><span># initialise the git repo
</span><span>git init
</span>
>现在,我可以安装和配置插件盖茨比所需的源,并显示我创建的文件。我现在将它们全部安装,并简要详细介绍它们的目的:
<span># create .gitignore file in my directory
</span><span>touch .gitignore
</span><span># add ignore contents with echo
</span><span>echo "# Project dependencies
</span><span>.cache
</span><span>node_modules
</span><span>
</span><span># Build directory
</span><span>public
</span><span>
</span><span># Other
</span><span>.DS_Store
</span><span>yarn-error.log" > .gitignore
</span>
<span>yarn add gatsby react react-dom
</span><span># -p is to create parent directories too if needed
</span><span>mkdir -p src/pages
</span><span># create the index (home) page
</span><span>touch src/pages/index.js
</span>
<span>import <span>React</span> from "react";
</span>
<span>export default function <span>IndexPage</span>() {
</span> <span>return <span><span><h1</span>></span>Hello, World!<span><span></h1</span>></span>;
</span><span>}
</span>
<span># if you're using npm ?
</span><span># $(npm bin)/gatsby develop
</span><span>yarn gatsby develop
</span>
>我将使用___graphql(三个下划线)路由来查看文件系统中的文件。<span>"scripts": {
</span> <span>"build": "gatsby build",
</span> <span>"dev": "gatsby develop",
</span> <span>"serve": "gatsby serve",
</span> <span>"clean": "gatsby clean"
</span><span>},
</span>
<span>import <span>{ RainbowText }</span> from './components/rainbow';
</span>## <span>A Markdown Heading
</span><span><span><span><RainbowText</span>></span>Wheeeeeeee<span><span></RainbowText</span>></span>
</span>
在gatsby-config.js文件中,还有一个可以指定站点元数据的选项。网站元数据是为了重复使用常见数据,例如网站标题和描述。
>
当我想在网站上添加元标记以进行搜索引擎优化(SEO)时,这将是进一步有用的。 (同样,稍后再详细介绍。
<span># create the project directory
</span><span>mkdir my-gatsby-blog
</span><span># change into the directory
</span><span>cd my-gatsby-blog
</span><span># initialise a package.json file
</span><span>yarn init -y
</span><span># initialise the git repo
</span><span>git init
</span>
<span># create .gitignore file in my directory
</span><span>touch .gitignore
</span><span># add ignore contents with echo
</span><span>echo "# Project dependencies
</span><span>.cache
</span><span>node_modules
</span><span>
</span><span># Build directory
</span><span>public
</span><span>
</span><span># Other
</span><span>.DS_Store
</span><span>yarn-error.log" > .gitignore
</span>
<span>yarn add gatsby react react-dom
</span><span># -p is to create parent directories too if needed
</span><span>mkdir -p src/pages
</span><span># create the index (home) page
</span><span>touch src/pages/index.js
</span>
<span>import <span>React</span> from "react";
</span>
<span>export default function <span>IndexPage</span>() {
</span> <span>return <span><span><h1</span>></span>Hello, World!<span><span></h1</span>></span>;
</span><span>}
</span>
<span># if you're using npm ?
</span><span># $(npm bin)/gatsby develop
</span><span>yarn gatsby develop
</span>
J
J <span>"scripts": {
</span> <span>"build": "gatsby build",
</span> <span>"dev": "gatsby develop",
</span> <span>"serve": "gatsby serve",
</span> <span>"clean": "gatsby clean"
</span><span>},
</span>
<span>"scripts": {
</span> <span>"build": "gatsby build",
</span> <span>"dev": "gatsby develop -p 8945 -o",
</span> <span>"serve": "gatsby serve -p 9854 -o",
</span> <span>"clean": "gatsby clean"
</span><span>},
</span>
<span>import <span>{ RainbowText }</span> from './components/rainbow';
</span>## <span>A Markdown Heading
</span><span><span><span><RainbowText</span>></span>Wheeeeeeee<span><span></RainbowText</span>></span>
</span>
<span># create the project directory
</span><span>mkdir my-gatsby-blog
</span><span># change into the directory
</span><span>cd my-gatsby-blog
</span><span># initialise a package.json file
</span><span>yarn init -y
</span><span># initialise the git repo
</span><span>git init
</span>
<span># create .gitignore file in my directory
</span><span>touch .gitignore
</span><span># add ignore contents with echo
</span><span>echo "# Project dependencies
</span><span>.cache
</span><span>node_modules
</span><span>
</span><span># Build directory
</span><span>public
</span><span>
</span><span># Other
</span><span>.DS_Store
</span><span>yarn-error.log" > .gitignore
</span>
>为主题UI添加一些其他依赖项,这些依赖项是:<span>yarn add gatsby react react-dom
</span><span># -p is to create parent directories too if needed
</span><span>mkdir -p src/pages
</span><span># create the index (home) page
</span><span>touch src/pages/index.js
</span>
<span>import <span>React</span> from "react";
</span>
<span>export default function <span>IndexPage</span>() {
</span> <span>return <span><span><h1</span>></span>Hello, World!<span><span></h1</span>></span>;
</span><span>}
</span>
<span># if you're using npm ?
</span><span># $(npm bin)/gatsby develop
</span><span>yarn gatsby develop
</span>
<span>"scripts": {
</span> <span>"build": "gatsby build",
</span> <span>"dev": "gatsby develop",
</span> <span>"serve": "gatsby serve",
</span> <span>"clean": "gatsby clean"
</span><span>},
</span>
<span>"scripts": {
</span> <span>"build": "gatsby build",
</span> <span>"dev": "gatsby develop -p 8945 -o",
</span> <span>"serve": "gatsby serve -p 9854 -o",
</span> <span>"clean": "gatsby clean"
</span><span>},
</span>
<span>import <span>{ RainbowText }</span> from './components/rainbow';
</span>## <span>A Markdown Heading
</span><span><span><span><RainbowText</span>></span>Wheeeeeeee<span><span></RainbowText</span>></span>
</span>
<span># create the project directory
</span><span>mkdir my-gatsby-blog
</span><span># change into the directory
</span><span>cd my-gatsby-blog
</span><span># initialise a package.json file
</span><span>yarn init -y
</span><span># initialise the git repo
</span><span>git init
</span>
<span># create .gitignore file in my directory
</span><span>touch .gitignore
</span><span># add ignore contents with echo
</span><span>echo "# Project dependencies
</span><span>.cache
</span><span>node_modules
</span><span>
</span><span># Build directory
</span><span>public
</span><span>
</span><span># Other
</span><span>.DS_Store
</span><span>yarn-error.log" > .gitignore
</span>
<span>yarn add gatsby react react-dom
</span><span># -p is to create parent directories too if needed
</span><span>mkdir -p src/pages
</span><span># create the index (home) page
</span><span>touch src/pages/index.js
</span>
>
然后,作为在日期下订单中订购帖子的一种方式,我添加了一种:allmdx(sort:{fields:[frontMatter__date],order:desc}){。这是在帖子前提下的日期进行排序。<span>import <span>{ RainbowText }</span> from './components/rainbow';
</span>## <span>A Markdown Heading
</span><span><span><span><RainbowText</span>></span>Wheeeeeeee<span><span></RainbowText</span>></span>
</span>
<span># create the project directory
</span><span>mkdir my-gatsby-blog
</span><span># change into the directory
</span><span>cd my-gatsby-blog
</span><span># initialise a package.json file
</span><span>yarn init -y
</span><span># initialise the git repo
</span><span>git init
</span>
<span># create .gitignore file in my directory
</span><span>touch .gitignore
</span><span># add ignore contents with echo
</span><span>echo "# Project dependencies
</span><span>.cache
</span><span>node_modules
</span><span>
</span><span># Build directory
</span><span>public
</span><span>
</span><span># Other
</span><span>.DS_Store
</span><span>yarn-error.log" > .gitignore
</span>
<span>yarn add gatsby react react-dom
</span><span># -p is to create parent directories too if needed
</span><span>mkdir -p src/pages
</span><span># create the index (home) page
</span><span>touch src/pages/index.js
</span>
>我将使用Gatsby文件系统路由API获取我之前创建的帖子的文件路径。文件系统路由API是从我的GraphQl数据编程创建页面的一种方式。
<span>import <span>React</span> from "react";
</span>
<span>export default function <span>IndexPage</span>() {
</span> <span>return <span><span><h1</span>></span>Hello, World!<span><span></h1</span>></span>;
</span><span>}
</span>
<span># if you're using npm ?
</span><span># $(npm bin)/gatsby develop
</span><span>yarn gatsby develop
</span>
<span>"scripts": {
</span> <span>"build": "gatsby build",
</span> <span>"dev": "gatsby develop -p 8945 -o",
</span> <span>"serve": "gatsby serve -p 9854 -o",
</span> <span>"clean": "gatsby clean"
</span><span>},
</span>
<span># add everything for committing
</span><span>git add .
</span><span># commit to repo
</span><span>git commit -m 'init project'
</span>
<span># this creates the folders in the root of the project
</span><span>mkdir -p content/2021/03/{06/hello-world,07/second-post,08/third-post}
</span><span># create individual files
</span><span>touch content/2021/03/06/hello-world/index.mdx
</span><span>touch content/2021/03/07/second-post/index.mdx
</span><span>touch content/2021/03/08/third-post/index.mdx
</span>
<span><span>---
</span></span><span><span><span>title: Hello World - from mdx!
</span></span></span><span><span><span>date: 2021-03-06</span>
</span></span><span><span>---</span>
</span>
My first post!!
<span><span>## h2 Heading</span>
</span>
Some meaningful prose
<span><span>### h3 Heading</span>
</span>
Some other meaningful prose
<span>import <span>{ RainbowText }</span> from './components/rainbow';
</span>## <span>A Markdown Heading
</span><span><span><span><RainbowText</span>></span>Wheeeeeeee<span><span></RainbowText</span>></span>
</span>
<span># create the project directory
</span><span>mkdir my-gatsby-blog
</span><span># change into the directory
</span><span>cd my-gatsby-blog
</span><span># initialise a package.json file
</span><span>yarn init -y
</span><span># initialise the git repo
</span><span>git init
</span>
root包装器概念
<span># create .gitignore file in my directory
</span><span>touch .gitignore
</span><span># add ignore contents with echo
</span><span>echo "# Project dependencies
</span><span>.cache
</span><span>node_modules
</span><span>
</span><span># Build directory
</span><span>public
</span><span>
</span><span># Other
</span><span>.DS_Store
</span><span>yarn-error.log" > .gitignore
</span>
<span>yarn add gatsby react react-dom
</span><span># -p is to create parent directories too if needed
</span><span>mkdir -p src/pages
</span><span># create the index (home) page
</span><span>touch src/pages/index.js
</span>
<span>import <span>React</span> from "react";
</span>
<span>export default function <span>IndexPage</span>() {
</span> <span>return <span><span><h1</span>></span>Hello, World!<span><span></h1</span>></span>;
</span><span>}
</span>
<span># if you're using npm ?
</span><span># $(npm bin)/gatsby develop
</span><span>yarn gatsby develop
</span>
<span>"scripts": {
</span> <span>"build": "gatsby build",
</span> <span>"dev": "gatsby develop",
</span> <span>"serve": "gatsby serve",
</span> <span>"clean": "gatsby clean"
</span><span>},
</span>
>在src/pages/404.js文件中,我将添加一条消息:<span>"scripts": {
</span> <span>"build": "gatsby build",
</span> <span>"dev": "gatsby develop -p 8945 -o",
</span> <span>"serve": "gatsby serve -p 9854 -o",
</span> <span>"clean": "gatsby clean"
</span><span>},
</span>
<span># add everything for committing
</span><span>git add .
</span><span># commit to repo
</span><span>git commit -m 'init project'
</span>
<span># this creates the folders in the root of the project
</span><span>mkdir -p content/2021/03/{06/hello-world,07/second-post,08/third-post}
</span><span># create individual files
</span><span>touch content/2021/03/06/hello-world/index.mdx
</span><span>touch content/2021/03/07/second-post/index.mdx
</span><span>touch content/2021/03/08/third-post/index.mdx
</span>
在转到下一节之前,
git提交:<span><span>---
</span></span><span><span><span>title: Hello World - from mdx!
</span></span></span><span><span><span>date: 2021-03-06</span>
</span></span><span><span>---</span>
</span>
My first post!!
<span><span>## h2 Heading</span>
</span>
Some meaningful prose
<span><span>### h3 Heading</span>
</span>
Some other meaningful prose
<span><span>---
</span></span><span><span><span>title: Second Post!
</span></span></span><span><span><span>date: 2021-03-07</span>
</span></span><span><span>---</span>
</span>
This is my second post!
<span>import <span>{ RainbowText }</span> from './components/rainbow';
</span>## <span>A Markdown Heading
</span><span><span><span><RainbowText</span>></span>Wheeeeeeee<span><span></RainbowText</span>></span>
</span>
<span># create the project directory
</span><span>mkdir my-gatsby-blog
</span><span># change into the directory
</span><span>cd my-gatsby-blog
</span><span># initialise a package.json file
</span><span>yarn init -y
</span><span># initialise the git repo
</span><span>git init
</span>
<span># create .gitignore file in my directory
</span><span>touch .gitignore
</span><span># add ignore contents with echo
</span><span>echo "# Project dependencies
</span><span>.cache
</span><span>node_modules
</span><span>
</span><span># Build directory
</span><span>public
</span><span>
</span><span># Other
</span><span>.DS_Store
</span><span>yarn-error.log" > .gitignore
</span>
<span>yarn add gatsby react react-dom
</span><span># -p is to create parent directories too if needed
</span><span>mkdir -p src/pages
</span><span># create the index (home) page
</span><span>touch src/pages/index.js
</span>
>下一个位是可选的。 Markdown JSX允许React(JSX)组件包含在Markdown中。为了证明这一点,我将添加一个RainbowText组件,该组件将在动画周期中为某些颜色而动画。动画需要额外的依赖性: @emotion/react的键框。我现在要安装:
<span>import <span>React</span> from "react";
</span>
<span>export default function <span>IndexPage</span>() {
</span> <span>return <span><span><h1</span>></span>Hello, World!<span><span></h1</span>></span>;
</span><span>}
</span>
<span># if you're using npm ?
</span><span># $(npm bin)/gatsby develop
</span><span>yarn gatsby develop
</span>
<span>"scripts": {
</span> <span>"build": "gatsby build",
</span> <span>"dev": "gatsby develop",
</span> <span>"serve": "gatsby serve",
</span> <span>"clean": "gatsby clean"
</span><span>},
</span>
<span>"scripts": {
</span> <span>"build": "gatsby build",
</span> <span>"dev": "gatsby develop -p 8945 -o",
</span> <span>"serve": "gatsby serve -p 9854 -o",
</span> <span>"clean": "gatsby clean"
</span><span>},
</span>
<span># add everything for committing
</span><span>git add .
</span><span># commit to repo
</span><span>git commit -m 'init project'
</span>
<span>import <span>{ RainbowText }</span> from './components/rainbow';
</span>## <span>A Markdown Heading
</span><span><span><span><RainbowText</span>></span>Wheeeeeeee<span><span></RainbowText</span>></span>
</span>
<span># create the project directory
</span><span>mkdir my-gatsby-blog
</span><span># change into the directory
</span><span>cd my-gatsby-blog
</span><span># initialise a package.json file
</span><span>yarn init -y
</span><span># initialise the git repo
</span><span>git init
</span>
<span># create .gitignore file in my directory
</span><span>touch .gitignore
</span><span># add ignore contents with echo
</span><span>echo "# Project dependencies
</span><span>.cache
</span><span>node_modules
</span><span>
</span><span># Build directory
</span><span>public
</span><span>
</span><span># Other
</span><span>.DS_Store
</span><span>yarn-error.log" > .gitignore
</span>
<span>yarn add gatsby react react-dom
</span><span># -p is to create parent directories too if needed
</span><span>mkdir -p src/pages
</span><span># create the index (home) page
</span><span>touch src/pages/index.js
</span>
如果我想通过搜索引擎在互联网上找到我的内容,那么
>我要YARN添加组件以及其工作所需的依赖项:<span>import <span>React</span> from "react";
</span>
<span>export default function <span>IndexPage</span>() {
</span> <span>return <span><span><h1</span>></span>Hello, World!<span><span></h1</span>></span>;
</span><span>}
</span>
<span># if you're using npm ?
</span><span># $(npm bin)/gatsby develop
</span><span>yarn gatsby develop
</span>
<span>"scripts": {
</span> <span>"build": "gatsby build",
</span> <span>"dev": "gatsby develop",
</span> <span>"serve": "gatsby serve",
</span> <span>"clean": "gatsby clean"
</span><span>},
</span>
<span>"scripts": {
</span> <span>"build": "gatsby build",
</span> <span>"dev": "gatsby develop -p 8945 -o",
</span> <span>"serve": "gatsby serve -p 9854 -o",
</span> <span>"clean": "gatsby clean"
</span><span>},
</span>
<span>import <span>{ RainbowText }</span> from './components/rainbow';
</span>## <span>A Markdown Heading
</span><span><span><span><RainbowText</span>></span>Wheeeeeeee<span><span></RainbowText</span>></span>
</span>
<span># create the project directory
</span><span>mkdir my-gatsby-blog
</span><span># change into the directory
</span><span>cd my-gatsby-blog
</span><span># initialise a package.json file
</span><span>yarn init -y
</span><span># initialise the git repo
</span><span>git init
</span>
<span># create .gitignore file in my directory
</span><span>touch .gitignore
</span><span># add ignore contents with echo
</span><span>echo "# Project dependencies
</span><span>.cache
</span><span>node_modules
</span><span>
</span><span># Build directory
</span><span>public
</span><span>
</span><span># Other
</span><span>.DS_Store
</span><span>yarn-error.log" > .gitignore
</span>
>我将登录到我的github帐户,然后在右上角选择我的头像映像旁边的加上图标,然后选择新的存储库。
> vercel
然后,我提示我设置并部署新项目。我将使用
> Enter<span>import <span>React</span> from "react";
</span>
<span>export default function <span>IndexPage</span>() {
</span> <span>return <span><span><h1</span>></span>Hello, World!<span><span></h1</span>></span>;
</span><span>}
</span>
netlify
通过CLI与Netlify一起部署与Vercel的部署相同,但我要进行拖放创建。>
在下一页上,我将给它以下设置:分支:默认值
等待渲染做事,然后单击项目名称下方的链接以查看网站Live。更多的盖茨比插件可供选择以添加其他功能。如果您想添加更多,我将把这些留给您。例如:
>
>
>另一种选择是合理的,我也听到了关于的好消息。
><span>import <span>{ RainbowText }</span> from './components/rainbow';
</span>## <span>A Markdown Heading
</span><span><span><span><RainbowText</span>></span>Wheeeeeeee<span><span></RainbowText</span>></span>
</span>
<span># create the project directory
</span><span>mkdir my-gatsby-blog
</span><span># change into the directory
</span><span>cd my-gatsby-blog
</span><span># initialise a package.json file
</span><span>yarn init -y
</span><span># initialise the git repo
</span><span>git init
</span>
wrap!
>如何将图像添加到我的盖茨比MDX博客文章中?
>在盖茨比MDX博客文章中添加图像可以增强视觉吸引力并使您的内容更具吸引力。为此,您需要将图像文件导入到MDX文件中。首先,将图像文件放在盖茨比项目的“ SRC”目录中。然后,在您的MDX文件中,使用'../ path/to/image.jpg'导入ImageName导入图像。导入后,您可以使用“ ImageName”作为组件中使用MDX内容中的图像:
>我如何添加代码片段到我的gatsby mdx博客文章?
<code>您的代码在此处</code>
>目录可以使您的博客文章更具导航。 Gatsby MDX支持创建目录。您可以在GraphQl查询中使用“ tableofcontents”字段来生成内容表。该字段返回一系列标题及其各自的深度,您可以使用它们为目录创建嵌套列表。
>如何将SEO添加到我的Gatsby MDX博客中? Gatsby MDX允许您将SEO添加到博客文章中。您可以使用“ Gatsby-Plugin-React-Helmet”来管理博客文章的文档头。该插件允许您添加诸如标题,描述和元标记之类的元素,这些元素对于SEO很重要。
>
>分页可以改善用户体验通过使您的博客更容易浏览您的帖子。 Gatsby MDX支持分页。您可以使用“ Gatsby-Awesome-pagination”插件来创建分页页面。该插件为您的页面创建了分页索引和分页的上下文。>如何将注释添加到我的盖茨比MDX博客文章中?>添加评论部分可以增加对您的博客的参与度。 Gatsby MDX允许您在博客文章中添加评论。您可以使用DISQUS或Commento等第三方服务。这些服务提供了一个可以在布局组件中包含的脚本以启用评论。
>>社交共享按钮可以增加触手可及的范围。您的博客文章。 Gatsby MDX允许您添加社交共享按钮。您可以使用诸如gatsby-plugin-reacct-share”之类的插件来添加社交共享按钮。该插件支持各种社交媒体平台,例如Facebook,Twitter和LinkedIn。通过更容易找到特定帖子来博客。 Gatsby MDX支持添加搜索功能。您可以使用诸如“ gatsby-plugin-elasticlunr-search”之类的插件来添加搜索功能。该插件创建了您的帖子索引,可以使用关键字搜索。
>
>新闻通讯订阅表单可以帮助您构建一个电子邮件列表并保持读者的更新。 Gatsby MDX允许您添加新闻通讯订阅表格。您可以使用MailChimp或SendInblue等服务。这些服务提供了您可以嵌入布局组件以启用新闻通讯订阅的表格。
以上是如何使用Gatsby和MDX构建开发人员博客的详细内容。更多信息请关注PHP中文网其他相关文章!