mirror of
https://github.com/haiwen/seahub.git
synced 2025-08-02 07:47:32 +00:00
check heading increase (#3159)
This commit is contained in:
parent
5a6ab8ab07
commit
2ba927e7e2
@ -12,7 +12,7 @@ from rest_framework import status
|
||||
from seahub.api2.authentication import TokenAuthentication
|
||||
from seahub.api2.throttling import UserRateThrottle
|
||||
from seahub.api2.utils import api_error
|
||||
from seahub.utils.markdown_lint import check_header_one, check_heading_end_with
|
||||
from seahub.utils.markdown_lint import check_heading_one, check_heading_end_with, check_heading_increase
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -42,13 +42,18 @@ class MarkdownLintView(APIView):
|
||||
document_nodes = slate["document"]["nodes"]
|
||||
|
||||
# check h1
|
||||
header_one_issue_list = check_header_one(document_nodes)
|
||||
if len(header_one_issue_list) > 0:
|
||||
issue_list.extend(header_one_issue_list)
|
||||
heading_one_issue_list = check_heading_one(document_nodes)
|
||||
if len(heading_one_issue_list) > 0:
|
||||
issue_list.extend(heading_one_issue_list)
|
||||
|
||||
# check heading_end_with
|
||||
heading_end_issue_list = check_heading_end_with(document_nodes)
|
||||
if len(heading_end_issue_list) > 0:
|
||||
issue_list.extend(heading_end_issue_list)
|
||||
|
||||
# check heading_increase
|
||||
heading_increase_issue_list = check_heading_increase(document_nodes)
|
||||
if len(heading_increase_issue_list) > 0:
|
||||
issue_list.extend(heading_increase_issue_list)
|
||||
|
||||
return Response({"issue_list": issue_list}, status=status.HTTP_200_OK)
|
||||
|
@ -2,18 +2,18 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
|
||||
def check_header_one(document_nodes):
|
||||
def check_heading_one(document_nodes):
|
||||
|
||||
issue_list = []
|
||||
issue_count = 0
|
||||
position = []
|
||||
h1_count = 0
|
||||
position_list = []
|
||||
|
||||
for index, node in enumerate(document_nodes):
|
||||
for position, node in enumerate(document_nodes):
|
||||
if node["type"] == "header_one":
|
||||
issue_count += 1 # issue < 1: missing h1; issue > 1: multiple h1.
|
||||
position.append(index)
|
||||
h1_count += 1
|
||||
position_list.append(position)
|
||||
|
||||
if issue_count < 1:
|
||||
if h1_count < 1:
|
||||
issue = dict()
|
||||
issue["issue"] = "Missing h1."
|
||||
issue["issue_code"] = "missing_h1"
|
||||
@ -26,25 +26,75 @@ def check_heading_end_with(document_nodes):
|
||||
|
||||
issue_list = []
|
||||
issue_count = 0
|
||||
position = []
|
||||
position_list = []
|
||||
|
||||
for index, node in enumerate(document_nodes):
|
||||
for position, node in enumerate(document_nodes):
|
||||
if node["type"].startswith("header_") and (
|
||||
node["nodes"][0]["leaves"][0]["text"].endswith(":") or
|
||||
node["nodes"][0]["leaves"][0]["text"].endswith(":")):
|
||||
issue_count += 1
|
||||
position.append(index)
|
||||
position_list.append(position)
|
||||
|
||||
if issue_count > 0:
|
||||
issue = dict()
|
||||
issue["issue"] = "Heading end with colon."
|
||||
issue["issue_code"] = "heading_end_with_colon"
|
||||
issue["detail"] = []
|
||||
for index in position:
|
||||
for position in position_list:
|
||||
detail = dict()
|
||||
detail["position"] = index
|
||||
detail["position"] = position
|
||||
detail["description"] = "Trailing punctuation in heading should not be a colon."
|
||||
issue["detail"].append(detail)
|
||||
issue_list.append(issue)
|
||||
|
||||
return issue_list
|
||||
|
||||
|
||||
def check_heading_increase(document_nodes):
|
||||
""" Only check h1, h2, h3, h4. Don't check h5, h6.
|
||||
"""
|
||||
|
||||
issue_list = []
|
||||
issue_count = 0
|
||||
heading_list = []
|
||||
position_list = []
|
||||
|
||||
for position, node in enumerate(document_nodes):
|
||||
# init heading data, e.g. {"heading_level": 2, "position": 1}
|
||||
heading_dict = dict()
|
||||
if node["type"].startswith("header_one"):
|
||||
heading_dict["heading_level"] = 1
|
||||
heading_dict["position"] = position
|
||||
heading_list.append(heading_dict)
|
||||
elif node["type"].startswith("header_two"):
|
||||
heading_dict["heading_level"] = 2
|
||||
heading_dict["position"] = position
|
||||
heading_list.append(heading_dict)
|
||||
elif node["type"].startswith("header_three"):
|
||||
heading_dict["heading_level"] = 3
|
||||
heading_dict["position"] = position
|
||||
heading_list.append(heading_dict)
|
||||
elif node["type"].startswith("header_four"):
|
||||
heading_dict["heading_level"] = 4
|
||||
heading_dict["position"] = position
|
||||
heading_list.append(heading_dict)
|
||||
|
||||
for index, heading in enumerate(heading_list[1:]):
|
||||
# The level of the current heading minus the level of the previous heading
|
||||
if heading["heading_level"] - heading_list[index]["heading_level"] > 1:
|
||||
issue_count += 1
|
||||
position_list.append(heading["position"])
|
||||
|
||||
if issue_count > 0:
|
||||
issue = dict()
|
||||
issue["issue"] = "Heading increase irregular."
|
||||
issue["issue_code"] = "heading_increase_irregular"
|
||||
issue["detail"] = []
|
||||
for position in position_list:
|
||||
detail = dict()
|
||||
detail["position"] = position
|
||||
detail["description"] = "Heading levels should only increment by one level at a time."
|
||||
issue["detail"].append(detail)
|
||||
issue_list.append(issue)
|
||||
|
||||
return issue_list
|
||||
|
Loading…
Reference in New Issue
Block a user