Skip to content

Commit 0be42fe

Browse files
committed
fix: mixed use with markdown link
1 parent b587eef commit 0be42fe

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function parser(md: MarkdownIt, symbols: KeySymbols, options: RubyParserOptions)
4646
}
4747

4848
let found = false
49+
let baseEndPos = 0
4950

5051
while (state.pos < max) {
5152
if (
@@ -54,18 +55,34 @@ function parser(md: MarkdownIt, symbols: KeySymbols, options: RubyParserOptions)
5455
state.src.charCodeAt(state.pos - 2) === symbols[1]
5556
) {
5657
found = true
58+
baseEndPos = state.pos - 2
5759
break
5860
}
5961

6062
state.md.inline.skipToken(state)
6163
}
6264

6365
// part of rt not found
64-
if (!found || start + 1 === state.pos) { // reset state if not found
66+
if (!found || start + 1 === baseEndPos) { // reset state if not found
6567
state.pos = start
6668
return false
6769
}
6870

71+
let checkPos = start
72+
while (checkPos < baseEndPos) {
73+
// check for ](
74+
if (
75+
state.src.charCodeAt(checkPos) === symbols[1] &&
76+
checkPos + 1 < max &&
77+
state.src.charCodeAt(checkPos + 1) === symbols[3]
78+
) {
79+
// markdown link instead of ruby
80+
state.pos = start
81+
return false
82+
}
83+
checkPos++
84+
}
85+
6986
const contentPos = state.pos
7087
const content = state.src.slice(start + 1, contentPos - 2)
7188

test/MainTest.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ describe('Test Ruby', () => {
147147
})
148148
})
149149

150+
describe('Test for mixing with other tags', () => {
151+
it('Mixing with links', () => {
152+
const md = new MarkdownIt()
153+
md.use(rubyParser)
154+
const origin = '[link](http:link) [ruby]^(rubytop)'
155+
const result = md.render(origin).trim()
156+
157+
expect(result).toBe(
158+
'<p><a href="http:link">link</a> <ruby><rb>ruby</rb><rp>(</rp><rt>rubytop</rt><rp>)</rp></ruby></p>',
159+
)
160+
})
161+
})
162+
150163
describe('Test for abnormal situation', () => {
151164
describe('Abnormal ends', () => {
152165
it('Ends of rt not found', () => {

0 commit comments

Comments
 (0)