Skip to content

Commit

Permalink
Merge pull request #511 from liangliangyy/dev
Browse files Browse the repository at this point in the history
增加toc目录支持,close #509
  • Loading branch information
liangliangyy authored Oct 18, 2021
2 parents 7e96de0 + 79a0651 commit 38173bf
Show file tree
Hide file tree
Showing 9 changed files with 383 additions and 180 deletions.
13 changes: 2 additions & 11 deletions DjangoBlog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
@time: 2017/10/25 下午10:16
"""

from django.test import Client, RequestFactory, TestCase
from blog.models import Article, Category, Tag
from django.contrib.auth import get_user_model
from DjangoBlog.utils import get_current_site
from django.urls import reverse
import datetime
from django.test import TestCase

from DjangoBlog.utils import *


Expand Down Expand Up @@ -49,8 +45,3 @@ def test_utils(self):
}
data = parse_dict_to_url(d)
self.assertIsNotNone(data)
render = BlogMarkDownRenderer()
s = render.autolink('http://www.baidu.com')
self.assertTrue(s.find('nofollow') > 0)
s = render.link('http://www.baidu.com', 'test', 'test')
self.assertTrue(s.find('nofollow') > 0)
94 changes: 22 additions & 72 deletions DjangoBlog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@
import uuid
from hashlib import sha256

import mistune
import requests
from django.contrib.sites.models import Site
from django.core.cache import cache
from mistune import escape, escape_link
from pygments import highlight
from pygments.formatters import html
from pygments.lexers import get_lexer_by_name

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -93,82 +88,37 @@ def expire_view_cache(path, servername, serverport, key_prefix=None):
return False


def block_code(text, lang, inlinestyles=False, linenos=False):
'''
markdown代码高亮
:param text:
:param lang:
:param inlinestyles:
:param linenos:
:return:
'''
if not lang:
text = text.strip()
return u'<pre><code>%s</code></pre>\n' % mistune.escape(text)

try:
lexer = get_lexer_by_name(lang, stripall=True)
formatter = html.HtmlFormatter(
noclasses=inlinestyles, linenos=linenos
)
code = highlight(text, lexer, formatter)
if linenos:
return '<div class="highlight">%s</div>\n' % code
return code
except BaseException:
return '<pre class="%s"><code>%s</code></pre>\n' % (
lang, mistune.escape(text)
)


@cache_decorator()
def get_current_site():
site = Site.objects.get_current()
return site


class BlogMarkDownRenderer(mistune.Renderer):
'''
markdown渲染
'''
class CommonMarkdown:
@staticmethod
def _convert_markdown(value):
import markdown
md = markdown.Markdown(
extensions=[
'extra',
'codehilite',
'toc',
'tables',
]
)
body = md.convert(value)
toc = md.toc
return body, toc

def block_code(self, text, lang=None):
# renderer has an options
inlinestyles = self.options.get('inlinestyles')
linenos = self.options.get('linenos')
return block_code(text, lang, inlinestyles, linenos)

def autolink(self, link, is_email=False):
text = link = escape(link)

if is_email:
link = 'mailto:%s' % link
if not link:
link = "#"
site = get_current_site()
nofollow = "" if link.find(site.domain) > 0 else "rel='nofollow'"
return '<a href="%s" %s>%s</a>' % (link, nofollow, text)

def link(self, link, title, text):
link = escape_link(link)
site = get_current_site()
nofollow = "" if link.find(site.domain) > 0 else "rel='nofollow'"
if not link:
link = "#"
if not title:
return '<a href="%s" %s>%s</a>' % (link, nofollow, text)
title = escape(title, quote=True)
return '<a href="%s" title="%s" %s>%s</a>' % (
link, title, nofollow, text)


class CommonMarkdown():
@staticmethod
def get_markdown(value):
renderer = BlogMarkDownRenderer(inlinestyles=False)
def get_markdown_with_toc(value):
body, toc = CommonMarkdown._convert_markdown(value)
return body, toc

mdp = mistune.Markdown(escape=True, renderer=renderer)
return mdp(value)
@staticmethod
def get_markdown(value):
body, toc = CommonMarkdown._convert_markdown(value)
return body


def send_email(emailto, title, content):
Expand Down
16 changes: 9 additions & 7 deletions blog/models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import logging
from abc import ABCMeta, abstractmethod, abstractproperty
from abc import abstractmethod

from django.db import models
from django.urls import reverse
from django.conf import settings
from uuslug import slugify
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from DjangoBlog.utils import get_current_site
from DjangoBlog.utils import cache_decorator, cache
from django.db import models
from django.urls import reverse
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
from mdeditor.fields import MDTextField
from uuslug import slugify

from DjangoBlog.utils import cache_decorator, cache
from DjangoBlog.utils import get_current_site

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -94,6 +95,7 @@ class Article(BaseModel):
on_delete=models.CASCADE)
article_order = models.IntegerField(
'排序,数字越大越靠前', blank=False, null=False, default=0)
show_toc = models.BooleanField("是否显示toc目录", blank=False, null=False, default=False)
category = models.ForeignKey(
'Category',
verbose_name='分类',
Expand Down
23 changes: 16 additions & 7 deletions blog/static/blog/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2479,17 +2479,26 @@ li #reply-title {
}

.breadcrumb {
margin-bottom: 20px;
list-style: none;
border-radius: 4px;
margin-bottom: 20px;
list-style: none;
border-radius: 4px;
}

.breadcrumb > li {
display: inline-block;
display: inline-block;
}

.breadcrumb > li + li:before {
color: #ccc;
content: "/\00a0";
color: #ccc;
content: "/\00a0";
}

.breadcrumb > .active {
color: #777;
color: #777;
}

.break_line {
height: 1px;
border: none;
/*border-top: 1px dashed #f5d6d6;*/
}
Loading

0 comments on commit 38173bf

Please sign in to comment.