<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Mainroad on 格物堂</title><link>https://gewutang.cn/tags/mainroad/</link><description>Recent content in Mainroad on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Fri, 06 Jun 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/mainroad/index.xml" rel="self" type="application/rss+xml"/><item><title>百折千转折腾网站的日子</title><link>https://gewutang.cn/2025/06/06/building-a-website-through-countless-twists-and-turns/</link><pubDate>Fri, 06 Jun 2025 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2025/06/06/building-a-website-through-countless-twists-and-turns/</guid><description>&#10;&#10;&#10;&lt;p&gt;一直以来我都想创建一个单独的&lt;a href="https://rfinance.org.cn"&gt;&lt;em&gt;r-finance&lt;/em&gt;&lt;/a&gt;网站来向国内的人推广&lt;em&gt;R&lt;/em&gt;语言在金融领域的知识和实践。之前苦于没有精力和时间以及缺乏建设网站的经验没能去做。毕竟作为财务管理出身的人，虽然一直以来也会跟R、Python、SQL等编程语言打交道，但从未与搭建网站单刀相对。&lt;/p&gt;&#10;&lt;p&gt;转机就在最近。我在学习Hugo搭建网站的过程中，发现github和netlify结合可以很便捷地搭建网站。加之Hugo提供的主题非常丰富，完全可以满足我的网站需求。无需再等，我决定尝试一下。&lt;/p&gt;&#10;&lt;p&gt;这次搭建网站我使用的基础工具是Rstudio以及R包bolgdown。有了工具之后，我开始在脑海中构思网站的结构，并在茫茫模板之海中打捞自己中意的模板。终于天不负我，我找到一个叫 &lt;a href="https://github.com/Vimux/Mainroad"&gt;mainroad&lt;/a&gt;的主题，简介大方，十分贴合我的想象。&lt;/p&gt;&#10;&lt;p&gt;以下是我的建站步骤，十分丝滑。&lt;/p&gt;&#10;&lt;div id="创建和克隆github库" class="section level1"&gt;&#10;&lt;h1&gt;创建和克隆github库&lt;/h1&gt;&#10;&lt;p&gt;我先在github上建立一个名为&lt;a href="https://github.com/dengyishuo/r-finance"&gt;r-finance&lt;/a&gt;的库。创建库时选择自动生成readme文件、gitignore文件和license文件，同时选择master作为默认分支。之后，在Rstudio中操作克隆项目到本地。具体操作为：&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;Rstudio -&amp;gt;&amp;gt; tools -&amp;gt;&amp;gt; Terminal -&amp;gt;&amp;gt; New Terminal&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;输入以下命令将r-finance库克隆到本地。&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;git clone https://github.com/dengyishuo/r-finance.git&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;进入r-finance目录，设置.gitignore文件。这个文件作用极大，可以帮助我们忽略一些不必要的文件，避免提交到github上。以下是我设置的.gitignore文件内容：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;public/&#10;resources/&#10;.hugo_build.lock&#10;# History files&#10;.Rhistory&#10;.Rapp.history&#10;*.Rproj&#10;.Rprofile&#10;# Session Data files&#10;.RData&#10;.RDataTmp&#10;# User-specific files&#10;.Ruserdata&#10;# Output files from R CMD check&#10;/*.Rcheck/&#10;# RStudio files&#10;.Rproj.user/&#10;# knitr and R markdown default cache directories&#10;*_cache/&#10;/cache/&#10;# R Environment Variables&#10;.Renviron&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;如果未安装blogdown包的话，先在Rstudio中安装blogdown包，命令如下：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;install.packages(&amp;quot;blogdown&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;接下来在Rstudio中，将r-finance文件夹设定为当前工作目录，再使用blogdown包创建一个网站。命令如下：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;blogdown::new_site()&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;在bash中使用以下命令更新主题为mainroad：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;cd r-finance&#10;# 更新主题为子模块&#10;git submodule add https://github.com/vimux/mainroad.git themes/mainroad&#10;# 查看子模块状态&#10;git submodule status&#10;# 查看子模块内容&#10;git cat .gitsubmodules&#10;# 如果有异常，重新初始化&#10;# git submodule update --init --recursive&#10;# 删除旧主题&#10;rm -rf themes/hugo-lithium&#10;#删除mainroad主题内部的 .git 文件夹&#10;rm -rf themes/mainroad/.git &#10;# 将主题添加到git中&#10;git add themes/mainroad&#10;# commit 更改&#10;git commit -m &amp;quot;fix:include theme direct&amp;quot;&#10;# 将更改推送到github&#10;git push origin master&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="配置config.toml" class="section level1"&gt;&#10;&lt;h1&gt;配置config.toml&lt;/h1&gt;&#10;&lt;p&gt;在r-finance目录下创建一个config.toml文件，内容如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;baseurl = &amp;quot;https://rfinance.org.cn&amp;quot;&#10;title = &amp;quot;R-Finance中国&amp;quot;&#10;languageCode = &amp;quot;zh-CN&amp;quot;&#10;theme = &amp;quot;mainroad&amp;quot;&#10;disqusShortname = &amp;quot;&amp;quot; # Enable comments by entering your Disqus shortname&#10;googleAnalytics = &amp;quot;&amp;quot; # Enable Google Analytics by entering your tracking id&#10;&#10;[pagination]&#10; pagerSize = 10 # Number of posts per page&#10;&#10;[params]&#10; description = &amp;quot;rfinance&amp;#39;s Personal blog about everything&amp;quot; # Description of your site&#10; opengraph = true&#10; twitter_cards = false&#10; readmore = false # Show &amp;quot;Read more&amp;quot; button in list if true&#10; authorbox = true&#10; pager = true&#10; post_meta = [&amp;quot;date&amp;quot;, &amp;quot;categories&amp;quot;] # Order of post meta information&#10; mainSections = [&amp;quot;post&amp;quot;, &amp;quot;docs&amp;quot;]&#10; ignoreLogs = [&amp;quot;warning-goldmark-raw-html&amp;quot;] &#10;&#10;[params.logo]&#10; subtitle = &amp;quot;Just another site&amp;quot; # Logo subtitle&#10;&#10;[params.sidebar]&#10; home = &amp;quot;right&amp;quot; # Configure layout for home page&#10; list = &amp;quot;right&amp;quot; # Configure layout for list pages&#10; single = &amp;quot;right&amp;quot; # Configure layout for single pages&#10; # Enable widgets in given order&#10; widgets = [&amp;quot;search&amp;quot;, &amp;quot;recent&amp;quot;, &amp;quot;categories&amp;quot;, &amp;quot;taglist&amp;quot;]&#10;&#10;[params.widgets]&#10; recent_num = 5 # Set the number of articles in the &amp;quot;Recent articles&amp;quot; widget&#10; tags_counter = false # Enable counter for each tag in &amp;quot;Tags&amp;quot; widget (disabled by default)&#10;&#10;[params.author]&#10; name = &amp;quot;rfinance&amp;quot;&#10; bio = &amp;quot;rfinnace&amp;#39;s true identity is unknown. Maybe he is a successful blogger or writer. Nobody knows it.&amp;quot;&#10; avatar = &amp;quot;img/avatar.png&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="验证网站" class="section level1"&gt;&#10;&lt;h1&gt;验证网站&lt;/h1&gt;&#10;&lt;p&gt;下面来验证hugo是否可以正确生成网站，命令如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;rm -rf public&#10;git submodule update --init --recursive&#10;hugo --gc --minify&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;如果一切顺利，就可以将文件推送至github了。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="推送到github" class="section level1"&gt;&#10;&lt;h1&gt;推送到github&lt;/h1&gt;&#10;&lt;p&gt;推送至github之前，先将r-finance目录下的所有文件添加到git中，命令如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;git add .&#10;git commit -m &amp;quot;build website&amp;quot;&#10;git push origin master&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="配置netlify" class="section level1"&gt;&#10;&lt;h1&gt;配置netlify&lt;/h1&gt;&#10;&lt;p&gt;在r-finance目录下配置netlify.toml，内容如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;[build]&#10;command = &amp;#39;git submodule update --init --recursive &amp;amp;&amp;amp; hugo --gc --minify&amp;#39;&#10;publish = &amp;#39;public&amp;#39;&#10;&#10;[build.environment]&#10;HUGO_VERSION = &amp;#39;0.147.7&amp;#39;&#10;HUGO_ENV = &amp;#39;production&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;接下来在netlify上创建一个新的站点。具体步骤：&lt;/p&gt;&#10;&lt;ol style="list-style-type: decimal"&gt;&#10;&lt;li&gt;登陆自己的账号&lt;/li&gt;&#10;&lt;li&gt;选择Add new project&lt;/li&gt;&#10;&lt;li&gt;选择Import an existing project&lt;/li&gt;&#10;&lt;li&gt;选择github作为代码源&lt;/li&gt;&#10;&lt;li&gt;选择r-finance库&lt;/li&gt;&#10;&lt;li&gt;给项目取一个名字:r-finance（这个名字将决定网站在在netlify上的默认网址：&lt;a href="https://r-finance.netlify.app" class="uri"&gt;https://r-finance.netlify.app&lt;/a&gt;）&lt;/li&gt;&#10;&lt;li&gt;选择master分支。发布目录为&lt;code&gt;public&lt;/code&gt;。&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;接下来在netlify上配置构建命令和发布目录，构建命令为：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;git submodule update --init --recursive &amp;amp;&amp;amp; hugo --gc --minify&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;配置环境变量，添加变量：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;HUGO_VERSION = &amp;#39;0.147.7&amp;#39;&#10;HUGO_ENV = &amp;#39;production&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;选择 &lt;code&gt;deploy&lt;/code&gt; 即可。此时已经可以用&lt;a href="https://r-finance.netlify.app"&gt;https://r-finance.netlify.app&lt;/a&gt; 访问网站了。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="设置个性化域名" class="section level1"&gt;&#10;&lt;h1&gt;设置个性化域名&lt;/h1&gt;&#10;&lt;p&gt;去阿里云购买一个自己中意的域名，我购买的域名是&lt;a href="https://rfinance.org.cn"&gt;rfinance.org.cn&lt;/a&gt;。接下来在阿里云控制台中进入域名解析设置：&lt;/p&gt;&#10;&lt;p&gt;选择&lt;code&gt;快速添加解析&lt;/code&gt;：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;您的业务需求是：网站域名解析到另外的目标域名&lt;/li&gt;&#10;&lt;li&gt;请选择网站域名：&#10;&lt;ul&gt;&#10;&lt;li&gt;x-quant.com.cn（对应设置“@”主机记录）&lt;/li&gt;&#10;&lt;li&gt;www.x-quant.com.cn（对应设置“www”主机记录）&lt;/li&gt;&#10;&lt;/ul&gt;&lt;/li&gt;&#10;&lt;li&gt;请选择目标域名：&lt;a href="https://r-finance.netlify.app"&gt;r-finance.netlify.app&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;确定即可。&lt;/p&gt;&#10;&lt;p&gt;接下来在netlify上配置自定义域名。步骤为：&lt;/p&gt;&#10;&lt;ol style="list-style-type: decimal"&gt;&#10;&lt;li&gt;登录netlify，进入r-finance项目&lt;/li&gt;&#10;&lt;li&gt;点击Set up your project&lt;/li&gt;&#10;&lt;li&gt;点击Set up a custom domain&lt;/li&gt;&#10;&lt;li&gt;点击domain you already own&lt;/li&gt;&#10;&lt;li&gt;输入你的域名（如rfinance.org.cn）&lt;/li&gt;&#10;&lt;li&gt;点击Verify domain，将域名添加到netlify的域名列表中。&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;接下来在netlify上配置SSL证书，选择Let’s Encrypt证书即可。&lt;/p&gt;&#10;&lt;p&gt;等待一些时间，网站就可以正常访问了。&lt;/p&gt;&#10;&lt;/div&gt;&#10;</description></item></channel></rss>