Skip to content

Commit 3a625ac

Browse files
authored
🐛 FIX: skipSpacesBack and skipCharsBack methods (#86)
1 parent a5c28ff commit 3a625ac

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

markdown_it/rules_block/heading.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,10 @@ def heading(state: StateBlock, startLine: int, endLine: int, silent: bool):
4242

4343
# Let's cut tails like ' ### ' from the end of string
4444

45-
# maximum = state.skipSpacesBack(maximum, pos)
46-
# tmp = state.skipCharsBack(maximum, 0x23, pos) # #
47-
# if tmp > pos and isSpace(state.srcCharCode[tmp - 1]):
48-
# maximum = tmp
49-
# TODO the code above doesn't seem to work, but this does
50-
# we should check why the code above doesn't work though
51-
_max = len(state.src[:maximum].rstrip().rstrip(chr(0x23)))
52-
try:
53-
if isSpace(state.srcCharCode[_max - 1]):
54-
maximum = _max
55-
except IndexError:
56-
pass
45+
maximum = state.skipSpacesBack(maximum, pos)
46+
tmp = state.skipCharsBack(maximum, 0x23, pos) # #
47+
if tmp > pos and isSpace(state.srcCharCode[tmp - 1]):
48+
maximum = tmp
5749

5850
state.line = startLine + 1
5951

markdown_it/rules_block/state_block.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def isEmpty(self, line):
128128
"""."""
129129
return (self.bMarks[line] + self.tShift[line]) >= self.eMarks[line]
130130

131-
def skipEmptyLines(self, from_pos):
131+
def skipEmptyLines(self, from_pos: int) -> int:
132132
"""."""
133133
while from_pos < self.lineMax:
134134
try:
@@ -142,40 +142,40 @@ def skipEmptyLines(self, from_pos):
142142
from_pos += 1
143143
return from_pos
144144

145-
def skipSpaces(self, pos: int):
145+
def skipSpaces(self, pos: int) -> int:
146146
"""Skip spaces from given position."""
147147
while pos < len(self.src):
148148
if not isSpace(self.srcCharCode[pos]):
149149
break
150150
pos += 1
151151
return pos
152152

153-
def skipSpacesBack(self, pos: int, minimum: int):
153+
def skipSpacesBack(self, pos: int, minimum: int) -> int:
154154
"""Skip spaces from given position in reverse."""
155155
if pos <= minimum:
156156
return pos
157157
while pos > minimum:
158+
pos -= 1
158159
if not isSpace(self.srcCharCode[pos]):
159160
return pos + 1
160-
pos -= 1
161161
return pos
162162

163-
def skipChars(self, pos: int, code: int):
163+
def skipChars(self, pos: int, code: int) -> int:
164164
"""Skip char codes from given position."""
165165
while pos < len(self.src):
166166
if self.srcCharCode[pos] != code:
167167
break
168168
pos += 1
169169
return pos
170170

171-
def skipCharsBack(self, pos, code, minimum):
171+
def skipCharsBack(self, pos: int, code: int, minimum: int) -> int:
172172
"""Skip char codes reverse from given position - 1."""
173173
if pos <= minimum:
174174
return pos
175175
while pos > minimum:
176+
pos -= 1
176177
if code != self.srcCharCode[pos]:
177178
return pos + 1
178-
pos -= 1
179179
return pos
180180

181181
def getLines(self, begin: int, end: int, indent, keepLastLF):

0 commit comments

Comments
 (0)