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
2 changes: 1 addition & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Turns your local git repositories and their GitHub issues into static HTML, and serves them through Cloudflare Pages + Access as a code browser only you can read.

- **Read every repo in one place**: your local repos listed by root, with the code highlighted
- **HTML documents render in place**: a `.html` file in a repo is shown rendered, with its source on the same page. Its scripts run in a sandbox, cut off from codeview
- **HTML documents render in place**: a `.html` file in a repo is shown rendered, with its source on the same page. Its scripts run in a sandbox, cut off from codeview. The frame grows to fit its content, so a phone scrolls the page as one
- **All issues in one place too**: open issues from every repo in a single list
- **Multiple GitHub accounts**: pull issues from separate accounts, such as personal and work, and keep them apart
- **Light on a phone**: static HTML that reads without JavaScript, so it opens quickly while you're away
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
手元の git リポジトリ群と、その GitHub Issue を静的 HTML にまとめ、自分だけが読めるコードブラウザとして Cloudflare Pages + Access で配信する。

- **全リポを1か所で読める**: 手元のリポを置き場ごとに並べ、コードをハイライトして読める
- **HTML の資料もそのまま読める**: リポに置いた `.html` は描画した結果を出し、ソースも同じページで読める。中のスクリプトは sandbox で codeview から切り離す
- **HTML の資料もそのまま読める**: リポに置いた `.html` は描画した結果を出し、ソースも同じページで読める。中のスクリプトは sandbox で codeview から切り離す。iframe は中身の高さまで伸びるので、スマホでもスクロールが1本で済む
- **Issue も1か所にまとまる**: 全リポの未消化の Issue を1つの一覧にする
- **GitHub の複数アカウントに対応**: 個人用と仕事用のように、別々のアカウントの Issue を分けて取り込める
- **スマホで軽い**: JavaScript なしでも読める静的 HTML なので、外出先でもすぐ開ける
Expand Down
22 changes: 22 additions & 0 deletions codeview/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,28 @@ def cell(item, cls, arrow_l, arrow_r):
if (mark(n)) history.replaceState(null, '', '#ln-' + n);
});

// iframe を中身の高さまで伸ばし、スクロールを外側の1本にまとめる。届くまでは CSS の 80vh のまま
const frames = [...document.querySelectorAll('iframe.htmlview')];
if (frames.length) {
const CAP = 50000;
const state = new Map();
window.addEventListener('message', (ev) => {
const f = frames.find((x) => x.contentWindow === ev.source);
const h = ev.data && ev.data.codeviewHeight;
if (!f || typeof h !== 'number' || !isFinite(h) || h <= 0) return;
// 100vh に余白を足した中身は、伸ばすたびに同じ幅だけ伸び返す。同じ伸びが続いたら止める
const s = state.get(f) || { cur: f.offsetHeight, step: 0, same: 0, frozen: false };
if (s.frozen) return;
const step = h - s.cur;
s.same = step > 0 && step === s.step ? s.same + 1 : 0;
s.step = step;
if (s.same >= 2) { s.frozen = true; state.set(f, s); return; }
s.cur = Math.min(h, CAP);
state.set(f, s);
f.style.height = s.cur + 'px';
});
}

const wrap = document.getElementById('wrap');
if (wrap) {
const KEY = 'codeview.wrap';
Expand Down
20 changes: 18 additions & 2 deletions codeview/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,34 @@ def is_html(path: str) -> bool:
return path.lower().endswith((".html", ".htm"))


# sandbox の外からは中身の高さを測れないので、中から親へ送らせる。送るのは数値だけ。
# 末尾に足すので、閉じていない <script> や <!-- で終わるファイルでは呑まれ、高さは CSS の既定のまま残る
HTML_FIT_SCRIPT = (
"<script>(()=>{let last=0;const send=()=>{"
# scrollHeight は枠の高さを下回らないので、はみ出していなければ <html> の箱の高さで縮める
"const d=document.documentElement;"
"const h=Math.ceil(d.scrollHeight>innerHeight?d.scrollHeight:d.getBoundingClientRect().height);"
"if(h!==last){last=h;parent.postMessage({codeviewHeight:h},'*');}};"
"addEventListener('load',send);"
"if(window.ResizeObserver){const r=new ResizeObserver(send);"
"r.observe(document.documentElement);if(document.body)r.observe(document.body);}"
"send();})();</script>"
)


def render_html_page(path: str, data: bytes) -> tuple[str, str | None]:
"""描画した HTML の iframe と、折りたたんだソースを返す。

allow-same-origin を付けないので、中のスクリプトは origin が不透明になり、
codeview の Cookie や親ページに触れない。
codeview の Cookie や親ページに触れない。中身の末尾には高さを親へ知らせる
`HTML_FIT_SCRIPT` を足し、親(FILE_SCRIPT)が iframe を中身の高さまで伸ばす。
"""
text = data.decode("utf-8", "replace")
code, lang = render_code_lang(path, data)
body = (
'<iframe class="htmlview" sandbox="allow-scripts" '
f'title="{html.escape(path.split("/")[-1], quote=True)}" '
f'srcdoc="{html.escape(text, quote=True)}"></iframe>'
f'srcdoc="{html.escape(text + HTML_FIT_SCRIPT, quote=True)}"></iframe>'
f'<details class="htmlsrc"><summary>Source</summary>{code}</details>'
)
return body, lang
Expand Down
2 changes: 1 addition & 1 deletion codeview/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
.nowrap .hl pre > span.hit { min-width: calc(100% + 2rem); }

/* ---------- html ---------- */
/* 中身の高さは sandbox の外から測れないので、画面に対する固定の高さにして中で送る */
/* 中身の高さは FILE_SCRIPT が中から受け取って伸ばす。JS が無いときと届くまでは画面に対する固定の高さで中を送る */
.htmlview {
display: block; width: 100%; height: 80vh; margin: 1em 0;
border: 1px solid var(--line); border-radius: var(--r-sm); background: #fff;
Expand Down
3 changes: 2 additions & 1 deletion docs/guarantees/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
| 画像ファイルのページはその画像を `<img>` で表示する | `test_images_are_copied_and_shown` |
| 5 MiB を超える画像のページは画像を出さず `Too large to display` を出す | `test_images_are_copied_and_shown` |
| `.svg` のページはコードとして描く | `test_images_are_copied_and_shown`, `test_html_file_page_renders_in_sandboxed_iframe` |
| `.html` と `.htm` のファイルページは、ファイルの中身をそのまま `srcdoc` に入れた `<iframe>` を本文に出す | `test_html_file_page_renders_in_sandboxed_iframe` |
| `.html` と `.htm` のファイルページは、ファイルの中身の末尾に高さを親へ送るスクリプト(`HTML_FIT_SCRIPT`)を足しただけのものを `srcdoc` に入れた `<iframe>` を本文に出す | `test_html_file_page_renders_in_sandboxed_iframe` |
| `.html` と `.htm` のファイルページは、iframe の中身から送られた高さで iframe を伸ばすスクリプトを持つ(JavaScript が無ければ高さは画面の 80% のまま) | `test_html_file_page_renders_in_sandboxed_iframe` |
| `.html` と `.htm` のファイルページの iframe の `sandbox` 属性は、`allow-scripts` を含む | `test_html_file_page_renders_in_sandboxed_iframe` |
| `.html` と `.htm` のファイルページの iframe の `sandbox` 属性は、`allow-same-origin` を含まない | `test_html_file_page_renders_in_sandboxed_iframe` |
| `.html` と `.htm` のファイルページでは、ハイライトしたソースも JavaScript なしで(`<details>` で)読める | `test_html_file_page_renders_in_sandboxed_iframe` |
Expand Down
6 changes: 5 additions & 1 deletion tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from urllib.parse import unquote

from conftest import commit, gh_row, git
from codeview.render import HTML_FIT_SCRIPT


def test_scans_children_and_skips(site):
Expand Down Expand Up @@ -216,7 +217,10 @@ def test_html_file_page_renders_in_sandboxed_iframe(site):
page = (out / f"{name}.html").read_text()
[frame] = _frames(page)
# HTMLParser が属性の実体参照を戻すので、元の中身と比べられる
assert frame["srcdoc"] == HTML_DOC, name
assert frame["srcdoc"] == HTML_DOC + HTML_FIT_SCRIPT, name
# 中から送られた高さで親が iframe を伸ばす
assert "codeviewHeight" in HTML_FIT_SCRIPT and "postMessage" in HTML_FIT_SCRIPT
assert "addEventListener('message'" in page and "codeviewHeight" in page.split("</iframe>", 1)[1]
tokens = frame["sandbox"].split()
assert "allow-scripts" in tokens and "allow-same-origin" not in tokens
# ソースも JavaScript なしで読める形で同じページにある
Expand Down