sh 此脚本更新markdown文件的TOC。源自https://github.com/kubernetes/release/blob/master/lib/common.sh

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 此脚本更新markdown文件的TOC。源自https://github.com/kubernetes/release/blob/master/lib/common.sh相关的知识,希望对你有一定的参考价值。

#!/bin/bash
#
# Copyright 2017 The Kubernetes Authors All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

file=$1
begin_block="<!-- BEGIN MUNGE: GENERATED_TOC -->"
end_block="<!-- END MUNGE: GENERATED_TOC -->"
tmpfile=/tmp/tmp.md

declare -A count

while read level heading; do
  indent="$(echo $level |sed -e "s,^#,,g" -e 's,#,  ,g')"
  # make a valid anchor
  anchor=${heading,,}
  anchor=${anchor// /-}
  anchor=${anchor//[\.\?\*\,\/\[\]()]/}
  # Keep track of dups and identify
  if [[ -n ${count[$anchor]} ]]; then
    ((count[$anchor]++)) ||true
    anchor+="-${count[$anchor]}"
  else
    # initialize value
    count[$anchor]=0
  fi
  echo "${indent}- [$heading](#$anchor)"
done < <(egrep "^#+ " $file) > $tmpfile

# Insert new TOC
sed -ir "/^$begin_block/,/^$end_block/{
   /^$begin_block/{
	 n
	 r $tmpfile
   }
   /^$end_block/!d
   }" $file

rm -f $tmpfile

以上是关于sh 此脚本更新markdown文件的TOC。源自https://github.com/kubernetes/release/blob/master/lib/common.sh的主要内容,如果未能解决你的问题,请参考以下文章

sh 此脚本将完整Markdown文件的目录转换为HTML文件。

sh 此脚本将完整Markdown文件的目录转换为reStructuredText文件。

r 此脚本源自setup_bootcamp.sh并将R设置为up。

轻松搞定Markdown表格

markdown自动生成侧边栏TOC /目录

markdown目录为啥变成toc