Add search MFM

This commit is contained in:
Sol Fisher Romanoff 2022-07-08 13:33:03 +03:00
parent 409c0f854c
commit 3e53ff92c0
Signed by: nbsp
GPG Key ID: 9D3F2B64F2341B62
2 changed files with 27 additions and 0 deletions

View File

@ -52,5 +52,27 @@ export default {
}
},
},
{
name: 'mfmSearch',
level: 'block',
start (src) { return src.match(/[^\n]+ search/)?.index },
tokenizer (src, tokens) {
const rule = /^([^\n]+) search$/
const match = rule.exec(src)
if (match) {
const token = {
type: 'mfmSearch',
raw: match[0],
query: match[1].trim(),
tokens: [],
}
this.lexer.inline(token.text, token.tokens)
return token
}
},
renderer (token) {
return `<a href="https://google.com/search?q=${token.query}">${token.raw}</a>`
},
},
],
}

View File

@ -26,4 +26,9 @@ describe('marked-mfm', () => {
marked.use(markedMfm)
expect(marked('$[spin.alternate,speed=0.5s test]')).toBe('<p><span style="display: inline-block; animation: mfm-spin 0.5s linear infinite; animation-direction: alternate;" class="spin">test</span></p>\n')
})
test('search', () => {
marked.use(markedMfm)
expect(marked('syuilo thighs search')).toBe('<a href="https://google.com/search?q=syuilo thighs">syuilo thighs search</a>')
})
})