Skip to content

Commit

Permalink
feat: do not indent the first paragraph
Browse files Browse the repository at this point in the history
Close #36
  • Loading branch information
reuixiy committed Jan 16, 2020
1 parent 0c46faa commit eaefda4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ dropCap | drop cap? | boolean, override `enableDropCap` in `config.toml`, theme
dropCapAfterHr | drop cap after every horizontal rule tag? | boolean, override `enableDropCapAfterHr` in `config.toml`, theme only
deleteHrBeforeDropCap | delete horizontal rule tag before drop cap? | boolean, override `deleteHrBeforeDropCap` in `config.toml`, theme only
indent | indent instead of margin? | boolean, override `paragraphStyle` in `config.toml`, theme only
indentFirstParagraph | indent the first paragraph? | boolean, override `indentFirstParagraph` in `config.toml`, theme only
align | normal, justify, center | srting, if euqal to "normal", will override `enableJustify` in `config.toml`, theme only
original | original? You can add the following 8 terms if you set `false`. The `author` is required, other optional | boolean, override `original` in `config.toml`, theme only
author | author of original post | string, theme only
Expand Down
1 change: 1 addition & 0 deletions README.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ dropCap | drop cap? | boolean, override `enableDropCap` in `config.toml`, theme
dropCapAfterHr | drop cap after every horizontal rule tag? | boolean, override `enableDropCapAfterHr` in `config.toml`, theme only
deleteHrBeforeDropCap | delete horizontal rule tag before drop cap? | boolean, override `deleteHrBeforeDropCap` in `config.toml`, theme only
indent | indent instead of margin? | boolean, override `paragraphStyle` in `config.toml`, theme only
indentFirstParagraph | indent the first paragraph? | boolean, override `indentFirstParagraph` in `config.toml`, theme only
align | normal, justify, center | srting, if euqal to "normal", will override `enableJustify` in `config.toml`, theme only
original | original? You can add the following 8 terms if you set `false`. The `author` is required, other optional | boolean, override `original` in `config.toml`, theme only
author | author of original post | string, theme only
Expand Down
5 changes: 5 additions & 0 deletions config-examples/en-us/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,11 @@ uglyURLs = false

enableParagraphIndent = true

indentFirstParagraph = false
# Note: *global settings*
# `indentFirstParagraph` in post’s Front Matter
# has a higher priority than here

paragraphStyle = "margin"
# Note: margin or indent
# *global settings*
Expand Down
5 changes: 5 additions & 0 deletions config-examples/zh-cn/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,11 @@ uglyURLs = false
# 是否开启
enableParagraphIndent = true

# 是否缩进第一段(全局设置)
indentFirstParagraph = false
# 说明:文章的 Front Matter 中的 `indentFirstParagraph`
# 的优先级高于此处

# 分段样式(全局设置)
paragraphStyle = "margin"
# 说明:段间距式(margin)或段首缩排(indent)
Expand Down
43 changes: 36 additions & 7 deletions layouts/partials/components/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
{{- end -}}
{{- $enableDropCap := .Scratch.Get "enableDropCap" -}}
{{- if $enableDropCap -}}
{{- $regexPatternDropCap := `(<p)(>)([^<])(.+(</p>|\n))` -}}
{{- $regexPatternDropCap := `(<p)(>)(.)([^<]+)` -}}
{{- $regexReplacementDropCap := `$1 style="text-indent:0"$2<span class="drop-cap">$3</span>$4` -}}
{{- $firstParagraphOld := (delimit (findRE $regexPatternDropCap .Content 1) " ") -}}
{{- $firstParagraphNew := (replaceRE $regexPatternDropCap $regexReplacementDropCap $firstParagraphOld) -}}
Expand All @@ -65,18 +65,19 @@

<!-- Drop Cap After `<hr />` -->
{{- $Content := .Scratch.Get "Content" -}}
{{- $enableDropCapAfterHr := and .Site.Params.enableDropCap (.Params.dropCapAfterHr | default .Site.Params.enableDropCapAfterHr) -}}
{{- if ne .Type "poetry" -}}
{{- if and .Site.Params.enableDropCap (.Params.dropCapAfterHr | default .Site.Params.enableDropCapAfterHr) -}}
{{- if $enableDropCapAfterHr -}}
{{- if .Params.deleteHrBeforeDropCap | default .Site.Params.deleteHrBeforeDropCap -}}
{{- $replacement := `$3 style="text-indent:0"$4<span class="drop-cap">$5</span>$6` -}}
{{- $replacement := `$3 style="text-indent:0"$4<span class="drop-cap">$5</span>` -}}
{{- $.Scratch.Set "replacement" $replacement -}}
{{- else -}}
{{- $replacement := `$1$3 style="text-indent:0"$4<span class="drop-cap">$5</span>$6` -}}
{{- $replacement := `$1$3 style="text-indent:0"$4<span class="drop-cap">$5</span>` -}}
{{- $.Scratch.Set "replacement" $replacement -}}
{{- end -}}
{{- $replacement := .Scratch.Get "replacement" -}}

{{- $regexPatternDropCapAfterHr := `(\n(<hr />|<hr>))(\n\n?<p)(>)([^<])(.+(</p>|\n))` -}}
{{- $regexPatternDropCapAfterHr := `(\n(<hr />|<hr>))(\n<p)(>)(.)` -}}
{{- $regexReplacementDropCapAfterHr := $replacement -}}
{{- $Content := $Content | replaceRE $regexPatternDropCapAfterHr $regexReplacementDropCapAfterHr | safeHTML -}}
{{- .Scratch.Set "Content" $Content -}}
Expand Down Expand Up @@ -175,15 +176,43 @@

<!-- Paragraph Indent -->
{{- $Content := .Scratch.Get "Content" -}}
{{- $enableIndent := (and .Site.Params.enableParagraphIndent .Params.indent) | default (and .Site.Params.enableParagraphIndent (eq .Site.Params.paragraphStyle "indent")) -}}
{{- if ne .Type "poetry" -}}
{{- if (and .Site.Params.enableParagraphIndent .Params.indent) | default (and .Site.Params.enableParagraphIndent (eq .Site.Params.paragraphStyle "indent")) -}}
{{- if $enableIndent -}}
{{- $regexPatternIndent := `((</p>|<blockquote>)\n<p)(>)(.+(<br />|<br>))` -}}
{{- $regexReplacementIndent := `$1 style="text-indent:0;padding-left:2em;margin:1em 0"$3$4` -}}
{{- $Content := $Content | replaceRE $regexPatternIndent $regexReplacementIndent | safeHTML -}}
{{- .Scratch.Set "Content" $Content -}}
{{- end -}}
{{- end -}}

<!-- Do NOT Indent The First Paragraph -->
{{- $Content := .Scratch.Get "Content" -}}
{{- if and (not (.Params.indentFirstParagraph | default .Site.Params.indentFirstParagraph)) $enableIndent -}}
{{- if ne .Type "poetry" -}}
{{- if not $enableDropCap -}}
{{- $regex := `(<p)(>[^<]+)` -}}
{{- $replacement := `$1 style="text-indent:0"$2` -}}
{{- $firstParagraphOld := (delimit (findRE $regex .Content 1) " ") -}}
{{- $firstParagraphNew := (replaceRE $regex $replacement $firstParagraphOld) -}}
{{- $Content := replace $Content $firstParagraphOld $firstParagraphNew | safeHTML -}}
{{- .Scratch.Set "Content" $Content -}}
{{- end -}}
{{- $Content := .Scratch.Get "Content" -}}
{{- if not $enableDropCapAfterHr -}}
{{- $regex := `((</h[1-6]>|<hr>|<hr />)\n<p)(>)` -}}
{{- $.Scratch.Set "regex" $regex -}}
{{- else -}}
{{- $regex := `((</h[1-6]>)\n<p)(>)` -}}
{{- $.Scratch.Set "regex" $regex -}}
{{- end -}}
{{- $regex := .Scratch.Get "regex" -}}
{{- $replacement := `$1 style="text-indent:0"$3` -}}
{{- $Content := $Content | replaceRE $regex $replacement | safeHTML -}}
{{- .Scratch.Set "Content" $Content -}}
{{- end -}}
{{- end -}}

<!-- Dark Mode -->
{{- if .Site.Params.enableHighlight -}}
{{- if and .Site.Params.enableDarkMode (eq .Site.Params.defaultTheme "dark") -}}
Expand All @@ -209,4 +238,4 @@

<!-- Final Content -->
{{- $Content := .Scratch.Get "Content" -}}
{{- .Scratch.Set "Content" $Content -}}
{{- .Scratch.Set "Content" $Content -}}

0 comments on commit eaefda4

Please sign in to comment.