Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dist/
/tasks/
/resource/notes/
/resource/pr-notes/
/sample-book/testing.code-workspace
6 changes: 6 additions & 0 deletions sample-book/META-INF/container.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
48 changes: 48 additions & 0 deletions sample-book/OEBPS/ch_1.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>Chapter 1: The Cross-File Discovery</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<h1>Chapter 1: The Cross-File Discovery</h1>

<p>
Di era modern penerbitan buku digital, berkas bab utama sering kali memisahkan catatan kaki ke dalam dokumen terpisah untuk menjaga keringkasan naskah utama<a href="notes.xhtml#fn1" id="ref1" epub:type="noteref" role="doc-noteref"><sup>[1]</sup></a>.
</p>

<p>
Sayangnya, banyak konversi EPUB lawas sama sekali tidak menyertakan atribut semantik formal. Sebagian besar hanya menggunakan teks berangka di dalam kurung siku biasa<a href="notes.xhtml#note-2" id="ref2">[2]</a> tanpa atribut <code>epub:type="noteref"</code>.
</p>

<p>
Bahkan, ada pula penerbit yang hanya menggunakan tanda bintang tunggal tanpa tag superscript<a href="notes.xhtml#fn-star" id="ref-star">*</a> untuk memberikan keterangan tambahan khusus pembaca.
</p>

<p>
Tantangan lainnya muncul ketika target catatan di berkas akhir dibungkus di dalam paragraf dengan tautan kosong pembuka<a href="notes.xhtml#note_4" id="ref4"><sup>4</sup></a>.
</p>

<p>
Sebagai pembanding, reader previewer kita juga tetap harus mampu menampilkan catatan lokal di halaman yang sama dengan mulus<a href="#local-fn" id="ref-local"><sup>[5]</sup></a> tanpa kendala.
</p>

<p>
Uji skenario batas (Edge Case): tautan catatan yang mengarah ke ID yang sengaja tidak ada di berkas tujuan<a href="notes.xhtml#anchor-sengaja-hilang" id="ref-broken"><sup>[99]</sup></a>.
</p>

<p>
Uji skenario batas lokal (Intra-file Edge Case): tautan catatan lokal yang mengarah ke ID yang tidak ada di bab ini<a href="#anchor-lokal-hilang" id="ref-local-broken"><sup>[100]</sup></a>.
</p>

<hr style="margin: 3rem 0; border: none; border-top: 1px solid #e5e7eb;"/>

<!-- Local Footnote Section -->
<section class="footnotes">
<aside id="local-fn" epub:type="footnote">
<p>5. <strong>Catatan Lokal:</strong> Ini adalah contoh catatan kaki yang berada di berkas yang sama (intra-file anchor). <a href="#ref-local" role="doc-backlink">↩</a></p>
</aside>
</section>
</body>
</html>
18 changes: 18 additions & 0 deletions sample-book/OEBPS/content.opf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="pub-id">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>Sample EPUB - Cross-File Footnotes Demo</dc:title>
<dc:creator>XHTML Reader Previewer</dc:creator>
<dc:language>en</dc:language>
</metadata>
<manifest>
<item id="ch1" href="ch_1.xhtml" media-type="application/xhtml+xml"/>
<item id="notes" href="notes.xhtml" media-type="application/xhtml+xml"/>
<item id="style" href="style.css" media-type="text/css"/>
<item id="toc" href="toc.xhtml" properties="nav" media-type="application/xhtml+xml"/>
</manifest>
<spine>
<itemref idref="ch1"/>
<itemref idref="notes"/>
</spine>
</package>
42 changes: 42 additions & 0 deletions sample-book/OEBPS/notes.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>Endnotes</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<h1>Endnotes</h1>

<section class="footnotes" epub:type="footnotes">
<!-- Case 1: Standard EPUB 3 footnote -->
<aside id="fn1" epub:type="footnote">
<p>
1. <strong>[Standar EPUB 3]</strong> Catatan ini diekstrak langsung dari berkas terpisah (<code>notes.xhtml</code>) ke dalam floating popover tanpa memuat ulang chapter utama. <a href="ch_1.xhtml#ref1" role="doc-backlink">↩</a>
</p>
</aside>

<ol class="notes-list">
<!-- Case 2: Heuristic list item footnote -->
<li id="note-2">
<p>
2. <strong>[Deteksi Heuristik]</strong> Catatan ini membuktikan bahwa tautan berpola teks <code>[2]</code> tanpa atribut semantik formal tetap sukses dideteksi dan ditampilkan di popover. <a href="ch_1.xhtml#ref2" class="footnote-backref">↵</a>
</p>
</li>
</ol>

<!-- Case 3: Star symbol footnote -->
<div id="fn-star" class="footnote">
<p>
* <strong>[Simbol Bintang]</strong> Catatan tambahan untuk simbol bintang tunggal. Seluruh simbol pengembalian (<code>↑</code>) telah otomatis dibersihkan. <a href="ch_1.xhtml#ref-star">↑</a>
</p>
</div>

<!-- Case 4: Inline anchor inside paragraph -->
<p>
<a id="note_4" name="note_4"></a>
4. <strong>[Enclosing Paragraph]</strong> Catatan ini membuktikan bahwa jika atribut ID berada di dalam tag <code>&lt;a&gt;</code> pembuka, seluruh paragraf penjelas tetap berhasil diekstrak utuh. <a href="ch_1.xhtml#ref4">↩</a>
</p>
</section>
</body>
</html>
45 changes: 45 additions & 0 deletions sample-book/OEBPS/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
body {
font-family: Georgia, serif;
line-height: 1.8;
margin: 2rem auto;
max-width: 42rem;
padding: 0 1rem;
}

h1, h2 {
font-family: system-ui, -apple-system, sans-serif;
}

a {
color: #2563eb;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

sup {
font-size: 0.75em;
line-height: 0;
vertical-align: super;
}

.test-badge {
display: inline-block;
background: #e0f2fe;
color: #0369a1;
font-size: 0.8rem;
font-weight: 600;
padding: 0.15rem 0.5rem;
border-radius: 4px;
margin-right: 0.5rem;
}

.notes-list {
padding-left: 1.5rem;
}

.notes-list li {
margin-bottom: 1rem;
}
17 changes: 17 additions & 0 deletions sample-book/OEBPS/toc.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>Table of Contents</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<nav epub:type="toc" id="toc">
<h1>Table of Contents</h1>
<ol>
<li><a href="ch_1.xhtml">Chapter 1: The Cross-File Discovery</a></li>
<li><a href="notes.xhtml">Endnotes</a></li>
</ol>
</nav>
</body>
</html>
59 changes: 58 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import * as os from 'node:os';
import * as path from 'node:path';
import { sanitizeXhtml } from './xhtml';
import { sanitizeXhtml, extractElementById } from './xhtml';
import { readerShell, ReaderNavInfo } from './reader-shell';
import { locateEpubContainer, parseContainerXml, parseOpfXml, loadEpubToc, EpubPackage } from './epub';
import { StateManager, ReaderPreferences } from './state-manager';
Expand Down Expand Up @@ -304,6 +304,51 @@ async function attachReaderToWebview(
return;
}

if (isRequestFootnoteMessage(message)) {
void (async () => {
try {
const target = resolveChapterLink(message.href, currentSource, watchRoot, epubPackage?.opfDir);
if (!target || !target.fragment) {
void panel.webview.postMessage({
type: 'footnoteResponse',
requestId: message.requestId,
error: 'Missing target fragment'
});
return;
}

const fileBytes = await vscode.workspace.fs.readFile(target.source);
const rawXhtml = new TextDecoder('utf-8').decode(fileBytes);
const extractedHtml = extractElementById(rawXhtml, target.fragment);

if (!extractedHtml) {
void panel.webview.postMessage({
type: 'footnoteResponse',
requestId: message.requestId,
error: 'Anchor not found in target file'
});
return;
}

const targetDir = parentUri(target.source);
const rewritten = await rewriteResourceUris(extractedHtml, targetDir, panel.webview, epubPackage?.opfDir);

void panel.webview.postMessage({
type: 'footnoteResponse',
requestId: message.requestId,
html: rewritten
});
} catch (error) {
void panel.webview.postMessage({
type: 'footnoteResponse',
requestId: message.requestId,
error: String(error)
});
}
})();
return;
}

if (isOpenLinkMessage(message)) {
const target = resolveChapterLink(message.href, currentSource, watchRoot, epubPackage?.opfDir);
if (target) {
Expand Down Expand Up @@ -360,6 +405,18 @@ function isOpenLinkMessage(message: unknown): message is { type: 'openLink'; hre
&& typeof (message as { href?: unknown }).href === 'string';
}

function isRequestFootnoteMessage(message: unknown): message is {
type: 'requestFootnote';
href: string;
requestId: string;
} {
return typeof message === 'object'
&& message !== null
&& (message as { type?: unknown }).type === 'requestFootnote'
&& typeof (message as { href?: unknown }).href === 'string'
&& typeof (message as { requestId?: unknown }).requestId === 'string';
}

function isSavePreferencesMessage(message: unknown): message is {
type: 'savePreferences';
preferences: Partial<ReaderPreferences>;
Expand Down
Loading
Loading