mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-11-04 07:24:13 +00:00 
			
		
		
		
	format ruby for masto api
This commit is contained in:
		
							parent
							
								
									68adb6c05d
								
							
						
					
					
						commit
						408e2f824a
					
				
					 2 changed files with 100 additions and 6 deletions
				
			
		| 
						 | 
				
			
			@ -409,7 +409,9 @@ export class MfmService {
 | 
			
		|||
						}
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					case 'group': { // this is mostly a hack for `ruby`
 | 
			
		||||
					// hack for ruby, should never be needed because we should
 | 
			
		||||
					// never send this out to other instances
 | 
			
		||||
					case 'group': {
 | 
			
		||||
						const el = doc.createElement('span');
 | 
			
		||||
						appendChildren(node.children, el);
 | 
			
		||||
						return el;
 | 
			
		||||
| 
						 | 
				
			
			@ -593,11 +595,65 @@ export class MfmService {
 | 
			
		|||
			},
 | 
			
		||||
 | 
			
		||||
			async fn(node) {
 | 
			
		||||
				switch (node.props.name) {
 | 
			
		||||
					case 'group': { // hack for ruby
 | 
			
		||||
						const el = doc.createElement('span');
 | 
			
		||||
						await appendChildren(node.children, el);
 | 
			
		||||
						return el;
 | 
			
		||||
					}
 | 
			
		||||
					case 'ruby': {
 | 
			
		||||
						if (node.children.length === 1) {
 | 
			
		||||
							const child = node.children[0];
 | 
			
		||||
							const text = child.type === 'text' ? child.props.text : '';
 | 
			
		||||
							const rubyEl = doc.createElement('ruby');
 | 
			
		||||
							const rtEl = doc.createElement('rt');
 | 
			
		||||
 | 
			
		||||
							const rpStartEl = doc.createElement('rp');
 | 
			
		||||
							rpStartEl.appendChild(doc.createTextNode('('));
 | 
			
		||||
							const rpEndEl = doc.createElement('rp');
 | 
			
		||||
							rpEndEl.appendChild(doc.createTextNode(')'));
 | 
			
		||||
 | 
			
		||||
							rubyEl.appendChild(doc.createTextNode(text.split(' ')[0]));
 | 
			
		||||
							rtEl.appendChild(doc.createTextNode(text.split(' ')[1]));
 | 
			
		||||
							rubyEl.appendChild(rpStartEl);
 | 
			
		||||
							rubyEl.appendChild(rtEl);
 | 
			
		||||
							rubyEl.appendChild(rpEndEl);
 | 
			
		||||
							return rubyEl;
 | 
			
		||||
						} else {
 | 
			
		||||
							const rt = node.children.at(-1);
 | 
			
		||||
 | 
			
		||||
							if (!rt) {
 | 
			
		||||
								const el = doc.createElement('span');
 | 
			
		||||
								await appendChildren(node.children, el);
 | 
			
		||||
								return el;
 | 
			
		||||
							}
 | 
			
		||||
 | 
			
		||||
							const text = rt.type === 'text' ? rt.props.text : '';
 | 
			
		||||
							const rubyEl = doc.createElement('ruby');
 | 
			
		||||
							const rtEl = doc.createElement('rt');
 | 
			
		||||
 | 
			
		||||
							const rpStartEl = doc.createElement('rp');
 | 
			
		||||
							rpStartEl.appendChild(doc.createTextNode('('));
 | 
			
		||||
							const rpEndEl = doc.createElement('rp');
 | 
			
		||||
							rpEndEl.appendChild(doc.createTextNode(')'));
 | 
			
		||||
 | 
			
		||||
							await appendChildren(node.children.slice(0, node.children.length - 1), rubyEl);
 | 
			
		||||
							rtEl.appendChild(doc.createTextNode(text.trim()));
 | 
			
		||||
							rubyEl.appendChild(rpStartEl);
 | 
			
		||||
							rubyEl.appendChild(rtEl);
 | 
			
		||||
							rubyEl.appendChild(rpEndEl);
 | 
			
		||||
							return rubyEl;
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					default: {
 | 
			
		||||
						const el = doc.createElement('span');
 | 
			
		||||
						el.textContent = '*';
 | 
			
		||||
						await appendChildren(node.children, el);
 | 
			
		||||
						el.textContent += '*';
 | 
			
		||||
						return el;
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			},
 | 
			
		||||
 | 
			
		||||
			blockCode(node) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -47,12 +47,50 @@ describe('MfmService', () => {
 | 
			
		|||
		});
 | 
			
		||||
 | 
			
		||||
		test('ruby', () => {
 | 
			
		||||
			const input = '$[ruby some text ignore me]';
 | 
			
		||||
			const output = '<p><ruby>some<rp>(</rp><rt>text</rt><rp>)</rp></ruby></p>';
 | 
			
		||||
			assert.equal(mfmService.toHtml(mfm.parse(input)), output);
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		test('ruby2', () => {
 | 
			
		||||
			const input = '$[ruby *some text* ignore me]';
 | 
			
		||||
			const output = '<p><ruby><i>some text</i><rp>(</rp><rt>ignore me</rt><rp>)</rp></ruby></p>';
 | 
			
		||||
			assert.equal(mfmService.toHtml(mfm.parse(input)), output);
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		test('ruby 3', () => {
 | 
			
		||||
			const input = '$[ruby $[group *some* text] ignore me]';
 | 
			
		||||
			const output = '<p><ruby><span><i>some</i> text</span><rp>(</rp><rt>ignore me</rt><rp>)</rp></ruby></p>';
 | 
			
		||||
			assert.equal(mfmService.toHtml(mfm.parse(input)), output);
 | 
			
		||||
		});
 | 
			
		||||
	});
 | 
			
		||||
 | 
			
		||||
	describe('toMastoApiHtml', () => {
 | 
			
		||||
		test('br', async () => {
 | 
			
		||||
			const input = 'foo\nbar\nbaz';
 | 
			
		||||
			const output = '<p><span>foo<br>bar<br>baz</span></p>';
 | 
			
		||||
			assert.equal(await mfmService.toMastoApiHtml(mfm.parse(input)), output);
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		test('br alt', async () => {
 | 
			
		||||
			const input = 'foo\r\nbar\rbaz';
 | 
			
		||||
			const output = '<p><span>foo<br>bar<br>baz</span></p>';
 | 
			
		||||
			assert.equal(await mfmService.toMastoApiHtml(mfm.parse(input)), output);
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		test('escape', async () => {
 | 
			
		||||
			const input = '```\n<p>Hello, world!</p>\n```';
 | 
			
		||||
			const output = '<p><pre><code><p>Hello, world!</p></code></pre></p>';
 | 
			
		||||
			assert.equal(await mfmService.toMastoApiHtml(mfm.parse(input)), output);
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		test('ruby', async () => {
 | 
			
		||||
			const input = '$[ruby $[group *some* text] ignore me]';
 | 
			
		||||
			const output = '<p><ruby><span><span>*some*</span><span> text</span></span><rp>(</rp><rt>ignore me</rt><rp>)</rp></ruby></p>';
 | 
			
		||||
			assert.equal(await mfmService.toMastoApiHtml(mfm.parse(input)), output);
 | 
			
		||||
		});
 | 
			
		||||
	});
 | 
			
		||||
 | 
			
		||||
	describe('fromHtml', () => {
 | 
			
		||||
		test('p', () => {
 | 
			
		||||
			assert.deepStrictEqual(mfmService.fromHtml('<p>a</p><p>b</p>'), 'a\n\nb');
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue