refactor(plugins): stackoverflow rewrite to jsx

This commit is contained in:
Artemy 2024-05-13 18:42:12 +03:00
parent c2e6475624
commit 78e45f3c80
3 changed files with 50 additions and 14 deletions

View File

@ -1,5 +1,5 @@
import { HandlerInput, Route } from '@txtdot/sdk'; import { HandlerInput, Route } from '@txtdot/sdk';
import { parseHTML } from 'linkedom'; import { JSX } from '@txtdot/sdk';
async function questions( async function questions(
input: HandlerInput, input: HandlerInput,
@ -16,10 +16,15 @@ async function questions(
const answers = allAnswers.map((a) => postParser(a)); const answers = allAnswers.map((a) => postParser(a));
return { return {
content: `${question}<hr>${answers.length} answers <hr>${answers.join('<hr>')}`, content: (
textContent: `${ro.q.id}/${ro.q.slug}\nText output not supported`, // TODO <>
{question}
<hr />
{answers.length} answers <hr />
{answers.join(<hr />)}
</>
),
title, title,
lang: document.documentElement.lang,
}; };
} }
@ -36,12 +41,27 @@ function postParser(el: Element | null): string {
(el.querySelector('.user-details a') as HTMLAnchorElement)?.href || ''; (el.querySelector('.user-details a') as HTMLAnchorElement)?.href || '';
const userTitle = el.querySelector('.user-action-time')?.textContent || ''; const userTitle = el.querySelector('.user-action-time')?.textContent || '';
return `<h4>${userTitle}${ return (
userUrl ? ` by <a href="${userUrl}">${userName}</a>` : '' <h4>
}</h4>`; {userTitle}
{userUrl ? (
<>
by <a href={userUrl}>{userName}</a>
</>
) : (
<></>
)}
</h4>
);
}); });
return `<h3>${voteCount} votes</h3>${body}${footer.join('')}`; return (
<>
<h3>{voteCount} votes</h3>
{body}
{footer.join('')}
</>
);
} }
export default questions; export default questions;

View File

@ -1,5 +1,5 @@
import { HandlerInput, Route } from '@txtdot/sdk'; import { HandlerInput, Route } from '@txtdot/sdk';
import { parseHTML } from 'linkedom'; import { JSX } from '@txtdot/sdk';
async function users( async function users(
input: HandlerInput, input: HandlerInput,
@ -22,15 +22,28 @@ async function users(
const type = const type =
el.querySelector('.iconAnswer, .iconQuestion')?.textContent || ''; el.querySelector('.iconAnswer, .iconQuestion')?.textContent || '';
return `<strong>${type} (${votes}) </strong><a href="${url}">${title}</a>`; return (
<>
<strong>
{type} ({votes}){' '}
</strong>
<a href={url}>{title}</a>
</>
);
}) })
.join('<br/>'); .join(<br />);
return { return {
content: `${userInfo}<hr><h3>Top Posts</h3>${topPosts}`, content: (
<>
{userInfo}
<hr />
<h3>Top Posts</h3>
{topPosts}
</>
),
textContent: `${ro.q.id}/${ro.q.slug}\n`, // TODO textContent: `${ro.q.id}/${ro.q.slug}\n`, // TODO
title: document.querySelector('title')?.textContent || '', title: document.querySelector('title')?.textContent || '',
lang: document.documentElement.lang,
}; };
} }

View File

@ -23,7 +23,10 @@ export function createElement(
else return `${key}=${value}`; else return `${key}=${value}`;
}) })
.join(' '); .join(' ');
return `<${name} ${propsstr}>${content.join('')}</${name}>`;
return content.length === 0
? `<${name} ${propsstr}/>`
: `<${name} ${propsstr}>${content.join('')}</${name}>`;
} else if (typeof name === 'function') { } else if (typeof name === 'function') {
return name(props, ...content); return name(props, ...content);
} else { } else {