feat(home): link the homepage of every local project in the workspace

- server: GET /api/local-projects scans sibling folders for index.html
  (title from <title>/<h1>, generic titles fall back to the folder name,
  sorted by newest edit) and GET /projects/<folder>/ serves that folder
- both endpoints are loopback-only; node_modules/.git/.hermes blocked and
  path traversal rejected; WORKSPACE_DIR=none disables the feature
- home: new 'โปรเจคในเครื่อง' list (2 columns >=700px) that hides itself when
  the API is absent, so the live static site never shows fake rows
- SW skips /projects/* so other projects' files are never cached; version 2026.11.19.5
- docs: README/AGENTS/DESIGN cover the endpoints, the loopback rule and the layout
This commit is contained in:
Bkk1234 committed 2026-11-20 14:26:54 +07:00
1 parent b27acd40ee
commit db7724b237
9 files changed
+417 -5

No files matched your search

+58
View File
@@ -2,6 +2,16 @@
import { escapeHtml, hostOf, icon, normalizeUrl } from "../ui.js";
import { initReveal, splitWordsHtml } from "../motion.js";
import { fetchLocalProjects } from "../data.js";
const TH_DATE = new Intl.DateTimeFormat("th-TH", { day: "numeric", month: "short", year: "numeric" });
function formatDate(iso) {
try {
return TH_DATE.format(new Date(iso));
} catch {
return "";
}
}
/** สุ่มลำดับด้วย Fisher–Yates แล้วหยิบมา count ใบ (ไม่ซ้ำ) */
function sampleRandom(list, count) {
@@ -86,6 +96,40 @@ export function homeView(ctx) {
initReveal(gridEl);
}
function localRowHtml(item) {
const folder = escapeHtml(item.folder);
const entry = escapeHtml(item.entry || "index.html");
const title = escapeHtml(item.title || item.folder);
const date = formatDate(item.updated_at);
return `
<li class="local-row">
<a class="local-row__link" href="${escapeHtml(item.url || "#")}" target="_blank" rel="noopener noreferrer">
<span class="local-row__icon" aria-hidden="true">${icon("folder")}</span>
<span class="local-row__body">
<span class="local-row__title">${title}</span>
<span class="local-row__meta"><code>${folder}</code> · ${entry}${
date ? ` · แก้ล่าสุด ${date}` : ""
}</span>
</span>
<span class="local-row__go"><span class="local-row__go-text">เปิดหน้าแรก</span>${icon("arrowRight")}</span>
</a>
</li>`;
}
/** โปรเจคในเครื่อง — มีเฉพาะเมื่อเปิดผ่านเซิร์ฟเวอร์หลังบ้าน (ไม่ขึ้นบนเว็บจริง) */
async function loadLocalProjects() {
const box = root?.querySelector("[data-local]");
const list = root?.querySelector("[data-local-list]");
const countEl = root?.querySelector("[data-local-count]");
if (!box || !list) return;
const result = await fetchLocalProjects();
if (!root || !result.projects.length) return;
if (countEl) countEl.textContent = `${result.projects.length} โฟลเดอร์`;
list.innerHTML = result.projects.map(localRowHtml).join("");
box.hidden = false;
initReveal(box);
}
return {
mount(container) {
root = container;
@@ -150,6 +194,19 @@ export function homeView(ctx) {
</div>
<div data-grid aria-labelledby="projects-title"></div>
<section class="local-section" data-local hidden aria-labelledby="local-title">
<div class="section-head" data-reveal="fade">
<div>
<h2 class="section-head__title" id="local-title">โปรเจคในเครื่อง</h2>
<p class="section-head__sub">
เปิดหน้าแรกของแต่ละโฟลเดอร์ในเครื่องนี้โดยตรง — เห็นเฉพาะเครื่องที่รันเซิร์ฟเวอร์ ไม่ขึ้นบนเว็บจริง
</p>
</div>
<p class="section-head__count" data-local-count></p>
</div>
<ul class="local-list" data-local-list></ul>
</section>
</div>`;
gridEl = root.querySelector("[data-grid]");
@@ -176,6 +233,7 @@ export function homeView(ctx) {
});
renderGrid();
loadLocalProjects();
},
destroy() {
root = null;