djledda.de main
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.7 KiB

  1. import { createTextVNode, computed, defineComponent, h, inject, onServerPrefetch, ref, type VNode, watchEffect } from "vue";
  2. import { RouterLink } from "vue-router";
  3. import useAsyncState from "@/useAsyncState.ts";
  4. import useHead from "@/useHead.ts";
  5. import DJEmail from "@/DJEmail.tsx";
  6. import getDJAPI from "@/api.ts";
  7. export default defineComponent({
  8. name: "ge-deutsch-article",
  9. props: {
  10. articleName: {
  11. type: String,
  12. required: true,
  13. },
  14. },
  15. async setup(props) {
  16. const currentLang = ref<"en" | "de">("de");
  17. function clickBtn() {
  18. currentLang.value = currentLang.value === "en" ? "de" : "en";
  19. }
  20. const parseDom = inject(
  21. "dom-parse",
  22. (innerHTML: string) => Object.assign(document.createElement("div"), { innerHTML }),
  23. );
  24. const { result: articleContent, stateIsReady } = useAsyncState(
  25. "ge-deutsch-article-data",
  26. async ({ hostUrl }) => {
  27. const articleResponse = await fetch(`${hostUrl}/generative-energy/content/${props.articleName}.html`);
  28. const result = await articleResponse.text();
  29. return result;
  30. },
  31. );
  32. const {
  33. result: articleData,
  34. stateIsReady: articleDataReady,
  35. } = useAsyncState('article-data', ({hostUrl}) => getDJAPI(hostUrl, '/rp-articles'));
  36. const articleMetadata = computed(() => articleData.value?.find(_ => _.slug === props.articleName));
  37. const title = computed(() => {
  38. return articleContent.value?.split('<h1 lang="de">')[1].split("</h1>")[0] ?? 'Artikel';
  39. });
  40. useHead({ title });
  41. onServerPrefetch(() =>
  42. new Promise<void>((res) => {
  43. watchEffect(() => {
  44. if (title.value !== "") {
  45. res();
  46. }
  47. });
  48. })
  49. );
  50. function transformArticleNode(node: Node): VNode | string {
  51. if (node.nodeType === node.ELEMENT_NODE) {
  52. const el = node as Element;
  53. const attrs: Record<string, string> = {};
  54. const children = [...node.childNodes].map((_) => transformArticleNode(_));
  55. if (el.tagName === "P") {
  56. el.classList.add("text-slab");
  57. children.unshift(h("button", {
  58. class: "swap",
  59. onClick: (e) => {
  60. (e.target as HTMLButtonElement).parentElement?.classList.toggle("swap");
  61. },
  62. }, "↻"));
  63. }
  64. for (let i = 0; i < el.attributes.length; i++) {
  65. const item = el.attributes.item(i);
  66. if (item) {
  67. attrs[item.name] = item.value;
  68. }
  69. }
  70. if (el.tagName === "H1") {
  71. return h("header", attrs, h("h1", {}, children));
  72. } else {
  73. return h((node as Element).tagName, attrs, children);
  74. }
  75. } else {
  76. return createTextVNode(node.textContent ?? "");
  77. }
  78. }
  79. function ArticleContentTransformed() {
  80. if (articleContent.value) {
  81. const dom = parseDom(articleContent.value);
  82. return h("div", {}, [...dom.children].map((_) => transformArticleNode(_)));
  83. }
  84. return <div>Artikel lädt...</div>;
  85. }
  86. await Promise.all([stateIsReady, articleDataReady]);
  87. console.log(articleMetadata.value);
  88. return () => (
  89. <div class="ge-article">
  90. <div class="header">
  91. <RouterLink to={{ name: "GEDeutsch" }}>Zur Artikelübersicht</RouterLink>
  92. <button onClick={clickBtn}>
  93. Sprache auf <span>{currentLang.value === "en" ? "Deutsch" : "Englisch"}</span> umschalten
  94. </button>
  95. </div>
  96. <p class="text-slab">
  97. Bei dem untenstehenden Artikel handelt es sich um eine hobbymäßige, amateurhafte Übersetzung des
  98. Artikels „{ title.value }“ von Ray Peat. Bei Ungenauigkeiten oder Fehlübersetzungen freue ich mich über <DJEmail>eine Mail</DJEmail>!
  99. </p>
  100. { articleMetadata.value?.tags?.includes('in-arbeit') && <h5 class="baustelle">🚧 Bitte beachte, dass diese Übersetzung noch in Arbeit und darum nicht fertig ist! 🚧</h5> }
  101. <hr />
  102. <article class={`lang-${currentLang.value}`}>
  103. <ArticleContentTransformed />
  104. </article>
  105. </div>
  106. );
  107. },
  108. });