[Blogger] A plug-in that automatically generates a table of contents from headings
※Note
This article is an article that mechanically translates This URL into English.
In this article, I will introduce how to automatically generate a table of contents in Blogger.
Blogger is a very good blog service, such as “no ad display”, “custom domain available”, and “Can be monetized with Adsence” even though it is free.
However, like WordPress, you can not install the plug-in, so the functionality of the standard widget provided by Blogger may be lacking.
In particular, Blogger does not have the ability to automatically generate a table of contents.
This time, I created a plug-in that automatically generates a table of contents in Blogger.
By introducing this plug-in, you can easily generate a table of contents automatically, like WordPress “TOC +” plug-in.
In fact, the table of contents shown below in this article is automatically generated by the plug-in introduced this time.
Feature
- Automatically detect heading tags (h2, h3, h4 …) and automatically generate a table of contents
- jQuery not used (plain JavaScirpt)
- With link button of show / hide of table of contents
- Indexing is possible even if header tags (h2, h3, h4) are formed hierarchically
- Lightweight, compressed CSS, JavaScript
- Optional control of table of contents display
- Automatically assign paragraph numbers
Introduction of plug-in
Back up the theme
Just in case, let’s backup the current theme.
The backup method is as follows.
- From the Blogger administration screen, click [Theme]-> [Backup / Restore].
- Click Download theme to download the current theme
Edit HTML
- On the theme screen, click [Edit HTML] to display the HTML
- Add the following code just before
</ head>
<!-- [START] Start TOC plug-in-->
<b:if cond='data:blog.pageType == "item"'>
<script>
//Change the following options to your liking
var toc_options = {
target: ["h2", "h3", "h4"],
autoNumber: true,
condTargetCount: 2,
insertPosition: "firstHeadBefore",
showToc: true,
width: "auto",
marginTop: "20px",
marginBottom: "20px",
indent: "20px",
postBodySelector: ".widget.Blog"
};
//Please do not change this compressed Javascript
(function(i){var j=0;document.addEventListener("DOMContentLoaded",function(){var p=document.querySelector(toc_options.postBodySelector);if(p==null||typeof p==="undefined"){reutrn}if(toc_options.target.length==0){return}rootContent=h(toc_options,p);if(rootContent.children.length>=toc_options.condTargetCount){var q=c(rootContent);o(q)}});function h(q,p){var u=q.target.length;var t=function(E,D,w){var z=q.target[E];var x=E<u-1?q.target[E+1]:"";var y="toc_headline_"+(++j);var F=g(z,m(D),E+1,y);w.children.push(F);D.id=y;var A=f(D);if(x==""){return}while(true){if(A==null||typeof A==="undefined"){break}if(b(A)==z){break}if(b(A)==x){t(E+1,A,F)}else{var B=A.getElementsByTagName(x);for(var C=0;C<B.length;C++){t(E+1,B[C],F)}}var A=f(A)}};var r=g("ROOT","",0);var v=p.getElementsByTagName(q.target[0]);for(var s=0;s<v.length;s++){t(0,v[s],r,"")}return r}function c(s){var r=document.createElement("div");r.classList.add("b-toc-container");r.style.marginTop=toc_options.marginTop;r.style.marginBottom=toc_options.marginTop;if(toc_options.width=="100%"){r.style.display="block"}else{r.style.width=toc_options.width}var q=document.createElement("p");var w=document.createElement("span");var v=document.createElement("span");var u=document.createElement("span");v.classList.add("b-toc-show-wrap");u.classList.add("b-toc-show-wrap");var y=document.createElement("a");w.innerText="table of contents";v.innerText="[";u.innerText="]";y.href="javascript:void(0);";q.appendChild(w);q.appendChild(v);q.appendChild(y);q.appendChild(u);var t=function(z){var p=typeof z==="boolean"?z:e(r,"hide");if(p){y.innerText="hide";r.classList.remove("hide")}else{y.innerText="show";r.classList.add("hide")}};y.addEventListener("click",t);t(toc_options.showToc);var x=document.createElement("ul");s.children.forEach(function(z,p){n(x,z,(p+1)+"")});r.appendChild(q);r.appendChild(x);return r}function n(s,u,w){var p=document.createElement("li");var q=document.createElement("a");p.style.paddingLeft=toc_options.indent;q.href="#"+u.id;if(toc_options.autoNumber){var t=document.createElement("span");t.classList.add("toc-number");t.innerText=w}var v=document.createElement("span");v.classList.add("toc-text");v.innerText=u.text;if(toc_options.autoNumber){q.appendChild(t)}q.appendChild(v);p.appendChild(q);s.appendChild(p);if(u.children.length>0){var r=document.createElement("ul");p.appendChild(r);u.children.forEach(function(y,x){n(r,y,w+"."+(x+1))})}}function o(q){var r=null;var p=document.querySelector(toc_options.postBodySelector);if(toc_options.insertPosition=="firstHeadBefore"||toc_options.insertPosition=="firstHeadAfter"){r=p.querySelector(toc_options.target[0])}else{if(toc_options.insertPosition=="top"){r=p}}if(r==null){return}if(toc_options.insertPosition=="firstHeadBefore"){k(r,q)}else{if(toc_options.insertPosition=="firstHeadAfter"){a(r,q)}else{if(toc_options.insertPosition=="top"){k(r,q)}}}}function g(q,r,p,s){return{tagName:q,text:r,children:[],nestLevel:p,id:s}}function m(p){return p.innerText}function f(p){return p.nextElementSibling}function d(p){return p.previousElementSibling}function b(p){return p.tagName.toLowerCase()}function e(p,q){return p.classList.contains(q)}function l(p){return p.parentNode}function a(q,s){var r=l(q);var p=f(q);if(r!=null&&p!=null){r.insertBefore(s,p)}}function k(p,r){var q=l(p);if(q!=null){q.insertBefore(r,p)}}})(window);
</script>
<style type="text/css">
.b-toc-container{background:#f9f9f9;border:1px solid #aaa;padding:10px;margin-bottom:1em;width:auto;display:table;font-size:95%}.b-toc-container p{text-align:center;margin:0;padding:0}.b-toc-container ul{list-style-type:none;list-style:none;margin:0;padding:0}.b-toc-container>ul{margin:15px 0 0}.b-toc-container.hide>ul{display:none}.b-toc-container ul li{margin:0;padding:0 0 0 20px;list-style:none}.b-toc-container ul li:after,.b-toc-container ul li:before{background:0;border-radius:0;content:""}.b-toc-container ul li a{text-decoration:none;color:#008db7!important;font-weight:400}.b-toc-container ul li .toc-number{margin:0 .5em 0 0}.b-toc-container ul li .toc-text:hover{text-decoration:underline}
</style>
</b:if>
<!-- [END] END TOC plug-in-->
- After pasting the code, click Save Theme on the screen to save the content.
This completes the plug-in settings.
After saving, please look at your blog. Surely, I think that the table of contents is displayed on your blog.
option
You can specify options for the TOC auto-generation plug-in introduced this time. You can change the displayed contents of the table of contents by changing the options.
Introduce how to set options.
How to set options
The copied code has a part where toc_options = {...}
is written.
Edit this JSON directly to change the TOC display options.
For the setting contents of options, please refer to “Description of each option” described later.
var toc_options = {
target: ["h2", "h3", "h4"],
autoNumber: true,
condTargetCount: 2,
insertPosition: "firstHeadBefore",
showToc: true,
width: "auto",
marginTop: "20px",
marginBottom: "20px",
indent: "20px",
postBodySelector: ".widget.Blog"
};
Description of each option
- target
Specify a heading tag in the range of h1 to h6 to create a table of contents.
- autoNumber
Specifies whether the table of contents is automatically numbered.
When ** true ** is specified, hierarchical serial numbers are automatically assigned in the order of 1 → 1.1 → 1.1.1.
** false ** Do not display the serial number.
- condTargetCount
Specify the number of headings to display the table of contents.
For example, if you specify 2
, the table of contents is displayed when there are two or more top-level headings specified with thetarget
option.
-insertPosition
Specify the display position of the table of contents from the following three.
Value | Display position of table of contents |
---|---|
firstHeadBefore | Before the first heading |
firstHeadAfter | After the first heading |
top | top of article |
-showToc
Specifies the initial TOC display state.
When ** true ** is specified, the table of contents is displayed in the initial state.
If you specify ** false **, the table of contents will be closed.
-width
Specifies the width of the table of contents.
** auto ** Automatically adjust the width to the number of characters in the table of contents
** ○○% ** Width specified as a percentage (100% full width display TOC)
** ○○ px ** Specify the width in pixels
-marginTop
Specifies the margin at the top of the table of contents.
-marginBottom
Specifies the bottom margin of the table of contents.
-indent
Specifies the indent width for the table of contents.
- postBodySelector
※ This option is recommended to change only those who are familiar with CSS selectors and JavaScript.
Specify the tag of the article body, which is the creation range of the table of contents, with the CSS selector.
If heading tags (h2 to h4) are used in addition to the article text, setting this option will allow you to create a table of contents with only the heading tags in the article text.
Finally
There are many people who use WordPress even around you, but I’m glad if the number of people who use Blogger can be increased by one with this convenient plug-in.
0 件のコメント:
コメントを投稿