如何撰写新博客?

1 创建新博客文件

1.1 新建文档放哪?

新建博客一般新开一个子文件夹,把博客文档以及相关图片、pdf文件等资料都放在一起。

  • 如果撰写英文博客,文档放在文件夹content/en/post/
  • 如果撰写中文博客,文档放在文件夹content/zh/post/

1.2 新博客有哪些文件类型?

博客文件可保存为三种类型:

  • .md:一般的markdown文件,不能运行R程序;
  • .RmdR Markdown文件,可以跑R程序,直接输出代码和结果。缺点是需要更长运行时间,会产生html网页文件;
  • .Rmarkdown:也是R Markdown文件,但运行R的是Hugo而不是Pandoc,因此功能有一定限制,比如不能引用参考文献,数学公式要安装软件包xaringan(简单好用的网页幻灯片)才行。
Table 1.1: Differences among the three document formats.
Feature .Rmd .Rmarkdown .md
Run R code yes yes no
Bibliography yes no no
Task list maybe yes yes
MathJax yes maybe maybe
HTML widgets yes no no

1.3 博客文档的前置部分

对于.Rmd文档,可参考本博客:

---
title: 如何撰写新博客?
author: Fiona
date: '2020-01-26'
slug: blogdown
categories:
  - R
tags:
  - Blog
image:
  caption: ''
  focal_point: ''
output:
  blogdown::html_page:
    toc: true  #显示目录
    toc_depth: 2 #目录层次 
    number_sections: true  #章节编号
---

对于.md文档,可参考:

+++
date = "2016-04-20T10:00:00"
draft = false
tags = []
title = "Writing content with Markdown, LaTeX, and Shortcodes"
math = true
+++

2 如何运用Markdown, LaTeX和Shortcodes撰写博客内容?

本网站是用blogdown和HUGO建立的,本节内容基于academic主题。

Content can be written using Markdown, LaTeX math, and Hugo Shortcodes. Additionally, HTML may be used for advanced formatting. This article gives an overview of the most common formatting options.

2.1 Sub-headings

## Heading 2
### Heading 3
#### Heading 4
##### Heading 5
###### Heading 6

2.2 Emphasis

Italics with *asterisks* or _underscores_.

Bold with **asterisks** or __underscores__.

Combined emphasis with **asterisks and _underscores_**.

Strikethrough with ~~two tildes~~.

2.3 Ordered lists

1. First item
2. Another item

2.4 Unordered lists

* First item
* Another item

2.5 Images

Images may be added to a page by placing them in your post folder and referencing them using one of the following two notations:

A general image:

![](./music.png)
Beautiful music

Beautiful music

A unified approach by knitr:

knitr::include_graphics('music.png')
插入本地图形(带编号和标题)

Figure 2.1: 插入本地图形(带编号和标题)

2.7 Emojis

See the Emoji cheat sheet for available emoticons. The following serves as an example, but you should remove the spaces between each emoji name and pair of semicolons:

I : heart : Academic : smile :

I ❤ Academic 😄

2.8 Blockquote

> This is a blockquote.

This is a blockquote.

2.9 Footnotes

I have more [^1] to say.

[^1]: Footnote example.

I have more [^1] to say. [^1]: Footnote example.

2.10 Code highlighting

Pass the language of the code, such as python, as a parameter after three backticks:

```python
# Example of code highlighting
input_string_var = input("Enter some data: ")
print("You entered: {}".format(input_string_var))
```

Result:

# Example of code highlighting
input_string_var = input("Enter some data: ")
print("You entered: {}".format(input_string_var))

2.10.1 Highlighting options

The Academic theme uses highlight.js for source code highlighting, and highlighting is enabled by default for all pages. However, several configuration options are supported that allow finer-grained control over highlight.js.

The following table lists the supported options for configuring highlight.js, along with their expected type and a short description. A “yes” in the config.toml column means the value can be set globally in config.toml, and a “yes” in the preamble column means that the value can be set locally in a particular page’s preamble.

option type description config.toml preamble
highlight boolean enable/disable highlighting yes yes
highlight_languages slice choose additional languages yes yes
highlight_style string choose a highlighting style yes no
highlight_version string choose the highlight.js version yes no

2.10.2 Option highlight

The highlight option allows enabling or disabling the inclusion of highlight.js, either globally or for a particular page. If the option is unset, it has the same effect as if you had specified highlight = true. That is, the highlight.js javascript and css files will be included in every page. If you’d like to only include highlight.js files on pages that actually require source code highlighting, you can set highlight = false in config.toml, and then override it by setting highlight = true in the preamble of any pages that require source code highlighting. Conversely, you could enable highlighting globally, and disable it locally for pages that do not require it. Here is a table that shows whether highlighting will be enabled for a page, based on the values of highlight set in config.toml and/or the page’s preamble.

config.toml page preamble highlighting enabled for page?
unset or true unset or true yes
unset or true false no
false unset or false no
false true yes

2.10.3 Option highlight_languages

The highlight_languages option allows you to specify additional languages that are supported by highlight.js, but are not considered “common” and therefore are not supported by default. For example, if you want source code highlighting for Go and clojure in all pages, set highlight_languages = ["go", "clojure"] in config.toml. If, on the other hand, you want to enable a language only for a specific page, you can set highlight_languages in that page’s preamble.

The highlight_languages options specified in config.toml and in a page’s preamble are additive. That is, if config.toml contains, highlight_languages = ["go"] and the page’s preamble contains highlight_languages = ["ocaml"], then javascript files for both go and ocaml will be included for that page.

If the highlight_languages option is set, then the corresponding javascript files will be served from the cdnjs server. To see a list of available languages, visit the cdnjs page and search for links with the word “languages”.

2.10.4 Option highlight_style

The highlight_style option allows you to select an alternate css style for highlighted code. For example, if you wanted to use the solarized-dark style, you could set highlight_style = "solarized-dark" in config.toml.

If the highlight_style option is unset, the default is to use the file /css/highlight.min.css, either the one provided by the Academic theme, or else the one in your local static directory. The /css/highlight.min.css file provided by Academic is equivalent to the github style from highlight.js.

If the highlight_style option is set, then /css/highlight.min.css is ignored, and the corresponding css file will be served from the cdnjs server. To see a list of available styles, visit the cdnjs page and search for links with the word “styles”.

See the highlight.js demo page for examples of available styles.

Not all styles listed on the highlight.js demo page are available from the cdnjs server. If you want to use a style that is not served by cdnjs, just leave highlight_style unset, and place the corresponding css file in /static/css/highlight.min.css.

If you don’t want to change the default style that ships with Academic but you do want the style file served from the cdnjs server, set highlight_style = “github" in config.toml.

The highlight_style option is only recognized when set in config.toml. Setting highlight_style in your page’s preamble has no effect.

2.10.5 Option highlight_version

The highlight_version option, as the name implies, allows you to select the version of highlight.js you want to use. The default value is “9.9.0”. The highlight_version option is only recognized when set in config.toml. Setting highlight_version in your page’s preamble has no effect.

2.11 Twitter tweet

To include a single tweet, pass the tweet’s ID from the tweet’s URL as parameter to the shortcode:

{{</* tweet 666616452582129664 */>}}

2.12 GitHub gist

{{</* gist USERNAME GIST-ID  */>}}

2.13 Speaker Deck

{{</* speakerdeck 4e8126e72d853c0060001f97 */>}}

2.14 \(\rm \LaTeX\) math

$$\left [ – \frac{\hbar^2}{2 m} \frac{\partial^2}{\partial x^2} + V \right ] \Psi = i \hbar \frac{\partial}{\partial t} \Psi$$

\[\left [ – \frac{\hbar^2}{2 m} \frac{\partial^2}{\partial x^2} + V \right ] \Psi = i \hbar \frac{\partial}{\partial t} \Psi\]

Alternatively, inline math can be written by wrapping the formula with only a single $:

This is inline: $\mathbf{y} = \mathbf{X}\boldsymbol\beta + \boldsymbol\varepsilon$

This is inline: \(\mathbf{y} = \mathbf{X}\boldsymbol\beta + \boldsymbol\varepsilon\)

2.15 Table

Code:

| Command           | Description                    |
| ------------------| ------------------------------ |
| `hugo`            | Build your website.            |
| `hugo serve -w`   | View your website.             |

Result:

Command Description
hugo Build your website.
hugo serve -w View your website.

2.16 Alerts

Alerts are a useful feature that add side content such as tips, notes, or warnings to your articles. They are especially handy when writing educational tutorial-style articles. Use the corresponding shortcodes to enable alerts inside your content:

{{% alert note %}}
Here's a tip or note...
{{% /alert %}}

This will display the following note block:

Here’s a tip or note…

{{% alert warning %}}
Here's some important information...
{{% /alert %}}

This will display the following warning block:

Here’s some important information…

3 如何在post中插入视频?

3.1 如何插入B站视频?

来源于下面帖子:https://www.shenxt.info/zh/post/blogdown/.

(1)点开想要插入的视频,点击分享.然后点击嵌入代码,复制代码,插入到markdown文件中即可.

(2)调整视频显示大小。直接嵌入的视频长宽不一定合适,需要手动调整.

首先设置宽度为100%,并设置一个id (如下设置ID为test):

<iframe id="test" src="//player.bilibili.com/player.html?aid=23877090&cid=39940519&page=1" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" width=100%> </iframe>

然后在markdown中插入如下JS代码:

<script type="text/javascript">
document.getElementById("test").style.height=document.getElementById("test").scrollWidth*0.76+"px";
</script>

结果如下:

3.2 如何在.md文件中插入视频?

3.2.1 A video from your static/img/ media library:

{{< video library="1" src="my_video.mp4" controls="yes" >}}

其中controls="yes"表示网页上可进行视频播放控制(否则打开网页就直接播放)。

3.2.2 A video within a page’s folder (e.g. content/post/hello/):

{{< video src="my_video.mp4" controls="yes" >}}

3.2.3 Youtube:

{{< youtube w7Ft2ymGmfc >}}

3.2.4 Vimeo:

{{< vimeo 146022717 >}}

3.3 如何在.Rmd文件中插入视频?

使用shortcode()函数.

3.3.1 本地视频

将视频放入在与post相同的文件夹中,然后插入下面语句(注意是要放在r代码框中):

blogdown::shortcode("video", src = "ren_stat.mp4", controls="yes")

3.3.2 Youtube视频

使用下面语句可以插入视频.第二个是youtube视频ID.

blogdown::shortcode("youtube", "p4liifrbK8w")
Avatar
Fei Wang
Student, Senior

生物医学工程, 医学影像

下一页
上一页