mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-24 11:01:17 -08:00
add underline with __
This commit is contained in:
7 files changed
+62
-4
No files matched your search
@@ -29,6 +29,14 @@ describe('bidirectional round-trip', () => {
|
||||
expect(result).toContain('*italic text*');
|
||||
});
|
||||
|
||||
it('round-trips underline', () => {
|
||||
const markdown = '__underlined__';
|
||||
const html = markdownToHtml(markdown);
|
||||
const injected = injectDataMd(html);
|
||||
const result = htmlToMarkdown(injected);
|
||||
expect(result).toContain('__underlined__');
|
||||
});
|
||||
|
||||
it('round-trips inline code', () => {
|
||||
const markdown = '`inline code`';
|
||||
const html = markdownToHtml(markdown);
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import type { TokenizerExtension, RendererExtension, Tokens } from 'marked';
|
||||
|
||||
// Underline extension: __text__
|
||||
export const matrixUnderlineExtension = {
|
||||
name: 'matrixUnderline',
|
||||
level: 'inline',
|
||||
start(src: string) {
|
||||
return src.indexOf('__');
|
||||
},
|
||||
tokenizer(
|
||||
this: {
|
||||
lexer: { inlineTokens: (t: string, tokens: Tokens.Generic[]) => void };
|
||||
},
|
||||
src: string
|
||||
) {
|
||||
if (!src.startsWith('__')) return undefined;
|
||||
const rule = /^__(.+?)__/;
|
||||
const match = rule.exec(src);
|
||||
if (match) {
|
||||
const token = {
|
||||
type: 'matrixUnderline',
|
||||
raw: match[0],
|
||||
text: match[1],
|
||||
tokens: [] as Tokens.Generic[],
|
||||
};
|
||||
this.lexer.inlineTokens(token.text!, token.tokens);
|
||||
return token;
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
renderer(
|
||||
this: { parser: { parseInline: (tokens: Tokens.Generic[]) => string } },
|
||||
token: Tokens.Generic
|
||||
) {
|
||||
const tokens = (token as { tokens: Tokens.Generic[] }).tokens || [];
|
||||
return `<u data-md="__">${this.parser.parseInline(tokens)}</u>`;
|
||||
},
|
||||
} satisfies TokenizerExtension & RendererExtension;
|
||||
@@ -117,8 +117,10 @@ function processNode(node: ChildNode, listDepth: number = 0): string {
|
||||
case 'i':
|
||||
return processInlineWrapper(node, '*');
|
||||
|
||||
case 'u':
|
||||
return processInlineWrapper(node, '_');
|
||||
case 'u': {
|
||||
const md = node.attribs['data-md'];
|
||||
return processInlineWrapper(node, md ?? '__');
|
||||
}
|
||||
|
||||
case 's':
|
||||
case 'del':
|
||||
|
||||
@@ -56,7 +56,7 @@ describe('injectDataMd', () => {
|
||||
|
||||
it('injects data-md into u tags', () => {
|
||||
const result = injectDataMd('<u>underline</u>');
|
||||
expect(result).toContain('data-md="_"');
|
||||
expect(result).toContain('data-md="__"');
|
||||
});
|
||||
|
||||
it('injects data-md into s tags', () => {
|
||||
|
||||
@@ -75,7 +75,7 @@ export function injectDataMd(html: string): string {
|
||||
// Inject inline markdown markers for underline
|
||||
html = html.replace(/<u([^>]*)>([^<]*)<\/u>/g, (_, attrs, content) => {
|
||||
if (attrs.includes('data-md')) return `<u${attrs}>${content}</u>`;
|
||||
return `<u data-md="_"${attrs}>${content}</u>`;
|
||||
return `<u data-md="__"${attrs}>${content}</u>`;
|
||||
});
|
||||
|
||||
// Inject inline markdown markers for strikethrough
|
||||
|
||||
@@ -13,6 +13,14 @@ describe('markdownToHtml', () => {
|
||||
expect(result).toContain('<strong>bold</strong>');
|
||||
});
|
||||
|
||||
it('converts __ to underline, not bold', () => {
|
||||
const result = markdownToHtml('__underlined__');
|
||||
expect(result).toContain('<u');
|
||||
expect(result).toContain('data-md="__"');
|
||||
expect(result).toContain('>underlined<');
|
||||
expect(result).not.toContain('<strong>underlined</strong>');
|
||||
});
|
||||
|
||||
it('converts italic text', () => {
|
||||
const result = markdownToHtml('*italic*');
|
||||
expect(result).toContain('<em>italic</em>');
|
||||
|
||||
@@ -4,12 +4,14 @@ import { matrixSpoilerExtension } from './extensions/matrix-spoiler';
|
||||
import { matrixMathExtension, matrixMathBlockExtension } from './extensions/matrix-math';
|
||||
import { matrixSubscriptExtension } from './extensions/matrix-subscript';
|
||||
import { matrixEmoticonExtension, preprocessEmoticon } from './extensions/matrix-emoticon';
|
||||
import { matrixUnderlineExtension } from './extensions/matrix-underline';
|
||||
import { unescapeMarkdownBlockSequences, unescapeMarkdownInlineSequences } from './utils';
|
||||
|
||||
// Configure marked with Matrix extensions
|
||||
const processor = marked.use({
|
||||
breaks: true,
|
||||
extensions: [
|
||||
matrixUnderlineExtension,
|
||||
matrixSpoilerExtension,
|
||||
matrixMathExtension,
|
||||
matrixMathBlockExtension,
|
||||
|
||||
Reference in new issue
Block a user