<!DOCTYPE html>
	<html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
	<head>
<title>Hướng dẫn làm mục lục tự động cho bài viết trong NukeViet</title>
<meta name="description" content="Hướng dẫn làm mục lục tự động cho bài viết trong NukeViet - Savefile - Tin tức -...">
<meta name="author" content="Thế Anh Group">
<meta name="copyright" content="Thế Anh Group">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="geo.region" content="VN-SG">
<meta name="geo.placename" content="Ho Chi Minh">
<meta name="geo.position" content="10.833111;106.641224">
<meta name="generator" content="NukeViet v4.0">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#00b456">
<meta name="msapplication-navbutton-color" content="#00b456">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#00b456">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Hướng dẫn làm mục lục tự động cho bài viết trong NukeViet">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin tức - https&#x3A;&#x002F;&#x002F;theanhgroup.com&#x002F;tin-tuc&#x002F;savefile&#x002F;thiet-ke-web&#x002F;huong-dan-lam-muc-luc-tu-dong-cho-bai-viet-trong-nukeviet-341.html">
<meta property="og:site_name" content="Thế Anh Group - Hosting - Domain - VPS">
<meta property="og:image" content="https://theanhgroup.com/uploads/bnn-tag.jpg">
<meta property="og:url" content="https://theanhgroup.com/tin-tuc/thiet-ke-web/huong-dan-lam-muc-luc-tu-dong-cho-bai-viet-trong-nukeviet-341.html">
<link rel="shortcut icon" href="https://theanhgroup.com/favicon.ico">
<link rel="preload" as="script" href="https://theanhgroup.com/assets/js/jquery/jquery.min.js">
<link rel="preload" as="script" href="https://theanhgroup.com/assets/js/language/vi.js">
<link rel="preload" as="script" href="https://theanhgroup.com/assets/js/global.js">
<link rel="preload" as="script" href="https://theanhgroup.com/themes/theanhgroup/js/news.js">
<link rel="preload" as="script" href="https://theanhgroup.com/themes/theanhgroup/js/main.js">
<link rel="preload" as="script" href="https://theanhgroup.com/OneSignalSDKWorker.js">
<link rel="preload" as="script" href="https://theanhgroup.com/OneSignalSDKUpdaterWorker.js">
<link rel="preload" as="script" href="https://cdn.onesignal.com/sdks/OneSignalSDK.js">
<link rel="preload" as="script" href="https://theanhgroup.com/themes/theanhgroup/js/intersection-observer.js">
<link rel="preload" as="script" href="https://theanhgroup.com/themes/theanhgroup/js/lazyload.min.js">
<link rel="preload" as="script" href="https://theanhgroup.com/themes/theanhgroup/js/bootstrap.min.js">
<link rel="stylesheet" href="https://theanhgroup.com/stylesheet/css/all.css">
<style>
	body{background: #fff;}
</style>
<script data-show="inline">(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-101091287-1', '.theanhgroup.com');
ga('send', 'pageview');
</script>
</head>
	<body >
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">Thế Anh Group - Hosting - Domain - VPS</h2>
		<p class="pull-right"><a title="Thế Anh Group - Hosting - Domain - VPS" href="https://theanhgroup.com/">https://theanhgroup.com</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Hướng dẫn làm mục lục tự động cho bài viết trong NukeViet</h1>
		<ul class="list-inline">
			<li>Thứ ba - 04/05/2021 09:42</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
			Tạo mục lục tự động cho bài viết trên website rất tốt cho việc tương tác trên website và tiện lợi cho người dùng, nhất là đối với những bài viết dàihơn nữa phần nào cũng giúp ích phần nào đó cho việc SEO website điều này được google đánh giá cao. Hôm nay Thế Anh Group sẽ chia sẻ cách làm điều đó cho website sử dụng mã nguồn NukeViet CMS
		</div>
		<div id="bodytext" class="clearfix">
			<h3>Tải file js đính kèm cuối bài viết</h3>

<p>Tài file zip đính kèm cuối bài viết giản nén ra file tocbot.min.js&nbsp;copy vào thư mục js của theme đang dùng</p>

<h3>Kiểm tra xem bài viết có đạt chuẩn để tạo mục lục hay không</h3>

<p>Để có được mục lục tự động thì yêu cầu trong bài viết phải có các thẻ từ h1, h2, h3, h4 vì vậy đoạn code sau sẽ làm điều đó</p>

<p>Mở file: <strong>modules/news/theme.php</strong></p>

<p>Tìm đến<strong> function detail_theme</strong></p>

<p>Thêm vào bên trên:</p>

<pre>
<code class="language-php">if (!empty($news_contents&#91;&#039;post_name&#039;&#93;)) {</code></pre>

<p>Đoạn code sau</p>

<pre>
<code>preg_match(&#039;|&lt;h&#91;^&gt;&#93;+&gt;(.*)&lt;/h&#91;^&gt;&#93;+&gt;|iU&#039;, $news_contents&#91;&#039;bodyhtml&#039;&#93;, $heading);
if(sizeof($heading) &gt; 0)
{
	$xtpl-&gt;parse(&#039;main.allowed_toc&#039;);
	$xtpl-&gt;parse(&#039;main.allowed_toc_js&#039;);
}</code></pre>

<h3>Thêm phần hiển thị ngoài site</h3>

<p>Ở đây ta dùng javascrip cụ thể là thư viện <strong>tocbot.min.js</strong> download đính kèm bên dưới bài viết</p>

<p>Giải nén và copy file <strong>tocbot.min.js</strong> vào thư mục <strong>theme/&lt;theme đang dùng&gt;/js</strong></p>

<p>Mở file <strong>theme/&lt;theme đang dùng&gt;/modules/news/detail.tpl</strong></p>

<pre>
<code class="language-html">&lt;!-- END: showhometext --&gt;</code></pre>

<p>Thêm bên dưới nó</p>

<pre>
<code class="language-html">&lt;!-- BEGIN: allowed_toc --&gt;
&lt;button class=&quot;btn-danh-sach-show&quot;&gt;&lt;i class=&quot;fa fa-list-ol&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;&lt;/button&gt;
&lt;div class=&quot;danh-sach-box&quot; style=&quot;margin-bottom:20px&quot;&gt;
	&lt;div class=&quot;danh-sach&quot;&gt;
		&lt;div class=&quot;head-danh-sach&quot;&gt;
			&lt;i class=&quot;fa fa-list-ol&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;Mục lục
			&lt;button class=&quot;btn-danh-sach-close&quot;&gt;&lt;i class=&quot;fa fa-times&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;&lt;/button&gt;
		&lt;/div&gt;
		&lt;div id=&quot;js-toc&quot; class=&quot;toc js-toc z-1 transition--300 pa4&quot;&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;
&lt;!-- END: allowed_toc --&gt;</code></pre>

<p>Tiếp đó tìm đến dòng</p>

<pre>
<code class="language-html">&lt;div id=&quot;news-bodyhtml&quot; class=&quot;bodytext margin-bottom-lg&quot;&gt;</code></pre>

<p>Sửa thành</p>

<pre>
<code class="language-html">&lt;div id=&quot;news-bodyhtml&quot; class=&quot;js-toc-content&quot;&gt;</code></pre>

<p>Tìm đến gần cuối file đoạn</p>

<pre>
<code class="language-html">&lt;!-- END: main --&gt;</code></pre>

<p>Thêm bên trên nó: (nhớ thay theanhgroup bằng tên theme đang dùng nhé !</p>

<pre>
<code class="language-html">&lt;!-- BEGIN: allowed_toc_js --&gt;
&lt;script type=&quot;text/javascript&quot; defer=&quot;async&quot; src=&quot;/themes/theanhgroup/js/tocbot.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
$(document).ready(function(){if($(window).width()&gt;768)var e=&quot;.danh-sach&quot;;var t=$(document).width()-($(&quot;#news-bodyhtml&quot;).offset().left+$(&quot;#news-bodyhtml&quot;).width())-140,s=$(document).width()-($(&quot;#news-bodyhtml&quot;).offset().left+$(&quot;#news-bodyhtml&quot;).width())-60,a=document.getElementsByClassName(&quot;danh-sach&quot;),o=document.getElementsByClassName(&quot;btn-danh-sach-show&quot;);if(a&#91;0&#93;.style.right=t+&quot;px&quot;,o&#91;0&#93;.style.right=s+&quot;px&quot;,$(&quot;.is-position-fixed .danh-sach&quot;).show(),$(&quot;.btn-danh-sach&quot;).click(function(){$(&quot;.js-toc&quot;).addClass(&quot;picaso&quot;)}),$(document).click(function(e){$(e.target).hasClass(&quot;btn-danh-sach&quot;)||0!==$(e.target).parents(&quot;.js-toc&quot;).length||$(&quot;.js-toc&quot;).removeClass(&quot;picaso&quot;)}),$(&quot;#news-bodyhtml h2, #news-bodyhtml h3, #news-bodyhtml h4, #news-bodyhtml h5, #news-bodyhtml h6&quot;).each(function(e){var t=$(this),s=t.text().toLowerCase().trim().replace(/&#91;\.,-\/#!?$%\^&amp;\*;:{}=\-_`~()&#93;/g,&quot;&quot;);s=(s=s.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,&quot;a&quot;).replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g,&quot;e&quot;).replace(/ì|í|ị|ỉ|ĩ/g,&quot;i&quot;).replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g,&quot;o&quot;).replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g,&quot;u&quot;).replace(/ỳ|ý|ỵ|ỷ|ỹ/g,&quot;y&quot;).replace(/đ/g,&quot;d&quot;).replace(/À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ/g,&quot;A&quot;).replace(/È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ/g,&quot;E&quot;).replace(/Ì|Í|Ị|Ỉ|Ĩ/g,&quot;I&quot;).replace(/Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ/g,&quot;O&quot;).replace(/Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ/g,&quot;U&quot;).replace(/Ỳ|Ý|Ỵ|Ỷ|Ỹ/g,&quot;Y&quot;).replace(/Đ/g,&quot;D&quot;)).replace(/\s+/g,&quot;-&quot;),t.attr(&quot;id&quot;,s)}),tocbot.init({tocSelector:&quot;.js-toc&quot;,contentSelector:&quot;.js-toc-content&quot;,headingSelector:&quot;h1, h2, h3, h4, h5&quot;,ignoreSelector:&quot;.js-toc-ignore&quot;,linkClass:&quot;toc-link&quot;,extraLinkClasses:&quot;tocextra&quot;,activeLinkClass:&quot;is-active-link&quot;,listClass:&quot;toc-list&quot;,extraListClasses:&quot;&quot;,isCollapsedClass:&quot;is-collapsed&quot;,collapsibleClass:&quot;is-collapsible&quot;,listItemClass:&quot;toc-list-item&quot;,collapseDepth:5,scrollSmooth:!0,scrollSmoothDuration:500,scrollEndCallback:function(e){},headingsOffset:-180,throttleTimeout:50,positionFixedSelector:e,positionFixedClass:&quot;is-position-fixed&quot;,fixedSidebarOffset:&quot;auto&quot;,includeHtml:!1,onClick:!1,orderedList:!0,scrollContainer:null,occho:170}),$(window).width()&gt;768){$flag=0,$(&quot;.danh-sach-box&quot;).height($(&quot;.danh-sach&quot;).outerHeight()),$(&quot;.btn-danh-sach-close&quot;).click(function(){$(&quot;.danh-sach&quot;).fadeOut(250),$flag=0}),$(&quot;.btn-danh-sach-show&quot;).click(function(){$(&quot;.danh-sach&quot;).fadeIn(250),$flag=1});var l=0;$(document).scroll(function(){l=$(this).scrollTop();var e=$(&quot;.danh-sach-box&quot;).offset().top+$(&quot;.danh-sach-box&quot;).height();l&gt;e?($(&quot;.btn-danh-sach-show&quot;).show(),0==$flag?$(&quot;.danh-sach&quot;).hide():$(&quot;.danh-sach&quot;).show()):($(&quot;.btn-danh-sach-show&quot;).hide(),$(&quot;.danh-sach&quot;).show(),$flag)})}});
&lt;/script&gt;
&lt;!-- END: allowed_toc_js --&gt;</code></pre>

<p>Mở file: <strong>theme/&lt;theme đang dùng&gt;/css/style.css</strong></p>

<p>Thêm vào cuối file:</p>

<pre>
<code class="language-css">.danh-sach {
	border: 2px solid #000;
    padding: 10px;
    background: #f3f3f3;
    margin-bottom: 20px;
    border-radius: 6px;

}

.danh-sach .fa {
	margin-right: 10px;
}

.head-danh-sach {
	background: none;
	border: none;
	width: 100%;
	font-weight: 700;
	text-transform: uppercase;
	text-align: left;
	font-size: 16px;
	padding: 5px 0;
}

.js-toc {
	margin-bottom: 10px;
    max-height: 400px;
    overflow-y: auto;
}

ol.toc-list {
	padding-left: 20px;
	list-style-type: decimal;
	margin-bottom: 0;
	margin-bottom: 5px;
}

ol.toc-list:last-child {
	margin-bottom: 0
}

ol.toc-list&gt;li:last-child {
	margin-bottom: 0
}

ol.toc-list a {
	width: 100%;
	background: linear-gradient(to right, #ddd 50%, #f3f3f3 50%);
	background-size: 200% 100%;
	background-position: right bottom;
	transition: all.25s ease;
	padding: 3px;
	display: inline-block;
}

ol.toc-list .is-active-link {
	background-position: left bottom;
}

ol.toc-list&gt;li&gt;a {
	font-weight: 700;
	color: #000
}

ol.toc-list&gt;li&gt;ol.toc-list&gt;li&gt;a {
	font-weight: 400
}

.is-position-fixed {
	position: fixed !important;
	top: 50px;
	z-index: 99
}

.is-position-fixed .js-toc {
	height: auto !important;
	width: 200px !important;
	visibility: visible !important;
	transition: .25s;
	visibility: hidden;
}

.picaso {
	transition: .25s;
}

.transition--300 {
	transition: all 300ms ease-in-out;
}

.btn-danh-sach-close {
	border: none;
	background: none;
	position: absolute;
	top: 13px;
	right: 0;
	width: 40px;
	padding: 0;
	display: none;
}

.btn-danh-sach-show {
	position: fixed;
	top: 50px;
	font-size: 20px;
	border: 2px solid #000;
	padding: 10px;
	background: #f3f3f3;
	line-height: 1;
	border-radius: 7px;
	z-index: 9;
	display: none;
	opacity: .7;
	transform: .25s
}

.btn-danh-sach-show:hover {
	opacity: 1;
	transform: .25s
}

.is-position-fixed .btn-danh-sach-close, .is-position-fixed .btn-danh-sach-show {
	display: block;
}

#bodyhtml h1:before, #bodyhtml h2:before, #bodyhtml h3:before, #bodyhtml h4:before, #bodyhtml h5:before, #bodyhtml h6:before {
    display: block;
    content: &quot; &quot;;
    height: 15px;
    margin-top: -15px;
    visibility: hidden;
}</code></pre>

<p>Hãy chú ý dòng này để điều chỉnh đích trượt đến cho phù hợp với website của mình</p>

<pre>
<code>#bodyhtml h1:before, #bodyhtml h2:before, #bodyhtml h3:before, #bodyhtml h4:before, #bodyhtml h5:before, #bodyhtml h6:before {
    display: block;
    content: &quot; &quot;;
    height: 15px;
    margin-top: -15px;
    visibility: hidden;
}</code></pre>

<p>Đây là cách làm của mình và nó có 1 hạn chế là….à mà thôi tự tìm hiểu nhé !</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://theanhgroup.com/tin-tuc/thiet-ke-web/huong-dan-lam-muc-luc-tu-dong-cho-bai-viet-trong-nukeviet-341.html" title="Hướng dẫn làm mục lục tự động cho bài viết trong NukeViet">https://theanhgroup.com/tin-tuc/thiet-ke-web/huong-dan-lam-muc-luc-tu-dong-cho-bai-viet-trong-nukeviet-341.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; Thế Anh Group - Hosting - Domain - VPS
		</div>
		<div id="contact">
			<a href="mailto:info@theanhgroup.com">info@theanhgroup.com</a>
		</div>
	</div>
</div>

		</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://theanhgroup.com/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
<div id="run_cronjobs" style="visibility:hidden;display:none;"><img alt="" src="/index.php?second=cronjobs&amp;p=k36047ys" width="1" height="1" /></div>
<script src="https://theanhgroup.com/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="tin-tuc",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=1,nv_is_recaptcha=1,nv_recaptcha_sitekey="6LcBa6sUAAAAAL3xxPH8QGCnxx1DtQN_Qu8zAtTm",nv_recaptcha_type="image",nv_recaptcha_elements=[];</script>
<script src="https://theanhgroup.com/assets/js/language/vi.js"></script>
<script src="https://theanhgroup.com/assets/js/global.js"></script>
<script src="https://theanhgroup.com/themes/theanhgroup/js/news.js"></script>
<script src="https://theanhgroup.com/themes/theanhgroup/js/main.js"></script>
<script type="application/ld+json">
		{
			"@context": "http://schema.org",
			"@type": "LocalBusiness",
			"url": "/",
			"logo": "/uploads/logo.svg",
			"image": "/uploads/logo.svg",
			"priceRange": "VND",
			"hasMap": "https://goo.gl/maps/GxvwWZZZJwYi28GR7",
			"email": "mailto: khachhang@theanhgroup.com",
			"address": { 
			"@type": "PostalAddress", 
			"addressLocality": "Gò Vấp", 
			"addressRegion": "Hồ Chí Minh", 
			"postalCode":"700000", 
			"streetAddress": "164/64 Bùi Quang Là, Q. Gò Vấp, TP. Hồ Chí Minh" 
			},
			"description": "Công Ty TNHH Thế Anh Group chuyên cung cấp giải pháp lưu trữ website, máy chủ ảo, tên miền giá rẻ uy tín hàng đầu Việt Nam",
			"name": "Công Ty TNHH Thế Anh Group", "telephone": "088-877-6611",
			"openingHours": [ "Mo-Fri 08:00-17:00", "Sat 08:00-12:00" ],
			"geo": { "@type": "GeoCoordinates", "latitude": "10.833485", "longitude": "106.641419" },
			"sameAs" : [ "https://www.facebook.com/TheAnhGroup/" , "https://www.pinterest.com/theanhgroup/"] 
		}
		</script>
<script src="https://theanhgroup.com/OneSignalSDKWorker.js" async=""></script>
<script src="https://theanhgroup.com/OneSignalSDKUpdaterWorker.js" async=""></script>
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
<script>
		  var OneSignal = window.OneSignal || [];
		  OneSignal.push(function() {
			OneSignal.init({
			  appId: "a62d202f-ca22-40ca-9987-e10da3bfbd9a",
			});
		  });
		</script>
<script>
		$(document).ready(function() {
			if (document.documentElement.clientWidth > 786 ) {
				window.onscroll = function() {myFunction()};
				var navbar = document.getElementById("main-menu");
				var sticky = navbar.offsetTop;
				function myFunction() {
					if (window.pageYOffset >= sticky) {
						navbar.classList.add("sticky")
					} else {
						navbar.classList.remove("sticky");
					}
				};
			}
			else
			{
				window.onscroll = function() {sticky_search_box()};
				var navbar_search = document.getElementById("box-search");
				var sticky_search = navbar_search.offsetTop;
				function sticky_search_box() {
					if (window.pageYOffset >= sticky_search + 300) {
						navbar_search.classList.add("sticky-search")
					} else {
						navbar_search.classList.remove("sticky-search");
					}
				}
			}
		});
		</script>
<script src="https://theanhgroup.com/themes/theanhgroup/js/intersection-observer.js"></script>
<script src="https://theanhgroup.com/themes/theanhgroup/js/lazyload.min.js"></script>
<script>
			var lazyLoadInstance = new LazyLoad({
				elements_selector: ".img-lazyload"
			}); 
		</script>
<script src="https://theanhgroup.com/themes/theanhgroup/js/bootstrap.min.js"></script>
</body>
</html>