feat: article titles and summaries stored apart from the text (v2.16.0, ADR-076)
Articles as the IndieWeb defines them: a name separate from the content.
- Migration 023 adds notes.title and notes.summary. A one-time Python data
step (migrations.DATA_MIGRATIONS) moves each note's '# Heading' first line
into title, rewrites the file and hash, empties FTS for a rebuild, and puts
files back if the migration fails. Trashed notes included.
- Micropub: name -> title, summary stored when sent with content, summary
updatable, q=source returns both. MCP tools take a summary.
- Feeds: titles only for articles (RSS/JSON none, Atom empty); Atom and
JSON Feed summaries; the title is no longer repeated in the content.
- Pages: p-name/p-summary on the article page. Homepage and tag pages show
articles as title, summary and "Read more" without images (hidden u-photo
keeps the post type for parsers).
- Crossposts: articles go out as title, summary, link and hashtags, no
images; the Bluesky card is the article. Notes unchanged.
- Editor: Title and Summary fields; an article may have no body.
- Fix: a line starting with a hashtag ('#indieweb ...') was rendered as a
heading and crossposted without its '#' (HashtagLineExtension).
- Fix: excerpts showed raw Markdown; they are plain text now.
Back up the database and the data folder before deploying: the migration
rewrites note files.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9a0d82bab2
commit
978c13358b
44 files changed
+943
-356
No files matched your search
@@ -7,6 +7,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [2.16.0] - 2026-11-11
|
||||
|
||||
Articles as the IndieWeb defines them: titles and summaries stored apart from the text (ADR-076).
|
||||
|
||||
**Before upgrading, back up the database and the `data` folder (notes and `.trash`).** Migration 023 moves each note's `# Heading` title out of its Markdown file into the database.
|
||||
|
||||
### Added
|
||||
|
||||
- Notes have a title and a summary of their own. A note with a title is an article. The editor has Title and Summary fields, and an article may have no body text
|
||||
- Micropub `summary` is stored when sent with `content`, can be replaced and deleted, and comes back from `q=source`. A summary on its own is still the content, as Post Type Discovery reads it. The MCP `create_note` and `update_note` tools take a `summary`
|
||||
- Article pages mark up the summary as `p-summary`; Atom entries have `<summary>` and JSON Feed items `summary`
|
||||
|
||||
### Changed
|
||||
|
||||
- A `# Heading` at the top of a note is now just a heading, not a title. Micropub `name` is stored as the title instead of being added to the text; a heading that repeats it is dropped
|
||||
- Feed items have a title only for articles. RSS items and JSON Feed items for notes have none, and Atom entries an empty one, so feed readers no longer show notes as headlines
|
||||
- On the homepage and tag pages an article shows its title, its summary (or the start of its text) and "Read more", without its images
|
||||
- Articles are crossposted to GoToSocial, Bluesky and Threads as title, summary (or the start of the text), link and hashtags, without images. On Bluesky the link card is the article. Notes are crossposted in full as before
|
||||
|
||||
### Fixed
|
||||
|
||||
- Feeds showed an article's title twice: as the item title and again at the top of the content
|
||||
- A line starting with a hashtag (`#indieweb is fun`) was shown as a heading on pages and in feeds, and crossposted without its `#`. Only `#` followed by a space starts a heading now, as in CommonMark
|
||||
- Previews, image alt text and crosspost teasers showed raw Markdown (`**bold**`, `[link](url)`); the excerpt is now plain text
|
||||
## [2.15.0] - 2026-11-11
|
||||
|
||||
MCP server for AI clients (ADR-075).
|
||||
|
||||
@@ -6,6 +6,8 @@ Accepted
|
||||
|
||||
> Its deferral of WebSub is superseded by ADR-071 (v2.10.0), and "no feed caching" by the feed cache added in v1.1.2. RSS is now one of three feed formats (ADR-038) and is served at `/feed.rss` (`/feed.xml` still works). (status updated 2026-11-03)
|
||||
|
||||
> Item titles: since v2.16.0 only articles (notes with a title) have one; notes have none, and no first-line or date title is made up (ADR-076).
|
||||
|
||||
## Context
|
||||
|
||||
Phase 5 requires implementing RSS feed generation for syndicating published notes. We need to decide on the implementation approach, feed format, caching strategy, and technical details for generating a standards-compliant RSS feed.
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
## Status
|
||||
Accepted (implemented in v1.3.0, reviewed in v2.8.0; was: Proposed)
|
||||
|
||||
> Since v2.16.0 `p-name` is the stored title and `p-summary` the author's summary or, in previews, the start of the text (ADR-076).
|
||||
|
||||
## Context
|
||||
StarPunk currently implements basic microformats2 markup:
|
||||
- h-entry on note articles
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
# ADR-076: Article titles and summaries stored apart from the text
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.16.0, 2026-11-11)
|
||||
|
||||
Supersedes the v2.3.3 design ([micropub-title-and-source-url](../design/v2.3.3/2026-10-31-micropub-title-and-source-url.md))
|
||||
and Q22's "a note has a title when its content starts with `#`". Amends
|
||||
ADR-014 (feed item titles) and ADR-040 (`p-name` and `p-summary`).
|
||||
|
||||
## Context
|
||||
|
||||
An IndieWeb article is an h-entry with a `name` that isn't just the start of
|
||||
its content (W3C Post Type Discovery). Since v1.2.0, StarPunk marked an article
|
||||
by a `# Heading` first line in the Markdown, and v2.3.3 stored a Micropub `name`
|
||||
as that heading. A review against IndieWeb practice found four gaps:
|
||||
|
||||
1. Feeds repeated an article's title: the item title, then the same heading
|
||||
rendered at the top of the content.
|
||||
2. Every feed item had a title, made from a note's first line (or the date),
|
||||
so feed readers showed notes as headlines and couldn't tell them from
|
||||
articles.
|
||||
3. Micropub `summary` was used only when `content` was missing; sent
|
||||
together with content, it was dropped.
|
||||
4. Any note that happened to start with a heading became an article, and its
|
||||
`q=source` changed from what the client sent. Micropub and mf2 treat `name`
|
||||
and `content` as separate properties.
|
||||
|
||||
## Decision
|
||||
|
||||
- **Columns.** `notes.title` and `notes.summary` (migration 023). A note with
|
||||
a title is an article; its Markdown file holds only the body. A heading in
|
||||
the body is just a heading.
|
||||
- **Moving existing titles.** Migration 023 has a Python data step
|
||||
(`migrations.DATA_MIGRATIONS`) that runs once, in the migration's
|
||||
transaction: for each note whose file starts with a heading (`split_title`,
|
||||
trashed files included), it moves the heading into `title`, rewrites the file
|
||||
and updates `content_hash`, then empties `notes_fts` so startup rebuilds it.
|
||||
If anything fails, the changed files are written back and the transaction is
|
||||
rolled back, so 023 is retried on the next start. Tying it to the migration
|
||||
matters: run on every start it would turn new notes' headings into titles.
|
||||
- **Micropub.** `name` → `title`; `summary` → `summary` when `content` is also
|
||||
sent. A summary on its own is still stored as the content, which is how Post
|
||||
Type Discovery reads it. `summary` can be replaced and deleted; `q=source`
|
||||
returns `name` and `summary` when set. If the content starts with a heading
|
||||
equal to `name`, that heading is dropped so it isn't shown twice.
|
||||
- **Feeds.** Only articles have a title: RSS leaves `<title>` out (an item
|
||||
needs a title or a description, and every item has a description), JSON Feed
|
||||
leaves `title` out, and Atom, which requires `<title>`, has an empty one.
|
||||
Atom `<summary>` and JSON Feed `summary` carry the author's summary. Content
|
||||
no longer contains the title, so it isn't repeated.
|
||||
- **Pages.** The article page has `p-name`, `p-summary` (when set) and the body
|
||||
as `e-content`. On the homepage and tag pages an article shows its title,
|
||||
summary (or the start of its text) and "Read more", without images. The
|
||||
photos stay in the markup as hidden `u-photo` values so parsers read the same
|
||||
post type there as on the article page.
|
||||
- **Crossposting.** Articles go to GoToSocial, Bluesky and Threads as title,
|
||||
summary (or the start of the text), link and hashtags, without images, the
|
||||
usual POSSE form for articles. On Bluesky the link card is the article
|
||||
itself. Notes are posted in full as before.
|
||||
- **Editor.** Title and Summary fields; a titled note may have no body.
|
||||
|
||||
## Consequences
|
||||
|
||||
- The migration rewrites note files. Back up the database *and* `data/notes`
|
||||
(and `.trash`) before deploying.
|
||||
- `Note.title` is `None` for notes. Places that need a name for any post use
|
||||
`Note.label` (title, else excerpt, else slug): notifications and the
|
||||
Bluesky card. `Note.has_title` and `Note.body_html` are gone.
|
||||
- Search indexes the title column; untitled notes are still listed by their
|
||||
first line.
|
||||
- A crossposted article that is edited is re-posted as a teaser.
|
||||
@@ -5,8 +5,8 @@ records one decision: its context, what was decided, and the consequences.
|
||||
Superseded ADRs are kept as history; their Status line names the ADR that
|
||||
replaced them.
|
||||
|
||||
**Total ADR files**: 71 (70 numbers: ADR-001 to ADR-044 and ADR-050 to
|
||||
ADR-075, with two files numbered ADR-012). See [Numbering](#numbering).
|
||||
**Total ADR files**: 72 (71 numbers: ADR-001 to ADR-044 and ADR-050 to
|
||||
ADR-076, with two files numbered ADR-012). See [Numbering](#numbering).
|
||||
|
||||
Status values: **Accepted** (in force), **Superseded** (replaced by a later
|
||||
ADR), **Deprecated** (withdrawn, nothing replaces it; none at present),
|
||||
@@ -58,7 +58,7 @@ See also [database architecture](../architecture/database.md).
|
||||
|
||||
| ADR | Title | Status | Summary |
|
||||
|---|---|---|---|
|
||||
| [014](ADR-014-rss-feed-implementation.md) | RSS Feed Implementation Strategy | Accepted* | RSS 2.0 feed; its WebSub deferral is superseded by ADR-071. |
|
||||
| [014](ADR-014-rss-feed-implementation.md) | RSS Feed Implementation Strategy | Accepted* | RSS 2.0 feed; its WebSub deferral is superseded by ADR-071, and item titles by ADR-076. |
|
||||
| [038](ADR-038-syndication-formats.md) | Multiple Syndication Format Support | Accepted | RSS, Atom and JSON Feed side by side (v1.1.2). |
|
||||
| [071](ADR-071-websub.md) | WebSub publishing with a built-in hub | Accepted | Built-in WebSub hub at `/websub` for StarPunk's own feeds (v2.10.0, amended v2.10.1). |
|
||||
| [059](ADR-059-full-feed-media-standardization.md) | Full Feed Media Standardization (Option 3) | Proposed | Full Media RSS, podcasts, video and thumbnails in feeds; not scheduled. |
|
||||
@@ -67,7 +67,8 @@ See also [database architecture](../architecture/database.md).
|
||||
|
||||
| ADR | Title | Status | Summary |
|
||||
|---|---|---|---|
|
||||
| [040](ADR-040-microformats2-compliance.md) | Strict Microformats2 Compliance | Accepted | Full h-entry, h-card and h-feed markup (v1.3.0, reviewed v2.8.0). |
|
||||
| [040](ADR-040-microformats2-compliance.md) | Strict Microformats2 Compliance | Accepted* | Full h-entry, h-card and h-feed markup (v1.3.0, reviewed v2.8.0); `p-name`/`p-summary` per ADR-076. |
|
||||
| [076](ADR-076-note-title-and-summary-columns.md) | Article titles and summaries stored apart from the text | Accepted | `notes.title`/`summary` columns; feeds title only articles; article previews and crossposts are title + summary (v2.16.0). |
|
||||
| [061](ADR-061-author-discovery.md) | Author Profile Discovery from IndieAuth | Accepted | The author h-card is read from `ADMIN_ME` and cached. |
|
||||
| [063](ADR-063-webmention-implementation.md) | Webmention Implementation | Accepted | Async receiving (202) and sending via a background worker, SSRF guard, rate limits, moderation (v1.6.0, revised up to v2.10.1). *Reconstructed.* |
|
||||
| [028](ADR-028-micropub-implementation.md) | Micropub Implementation Strategy | Accepted* | `/micropub` endpoint for creating posts (v1.0.0-rc.1); tokens now per ADR-064. |
|
||||
@@ -203,5 +204,5 @@ was carried out and is not itself superseded.
|
||||
|
||||
---
|
||||
|
||||
**Last Updated**: 2026-11-10
|
||||
**Total ADR files**: 70
|
||||
**Last Updated**: 2026-11-11
|
||||
**Total ADR files**: 72
|
||||
@@ -343,6 +343,8 @@ Priority order:
|
||||
3. First h-card found (fallback)
|
||||
|
||||
### p-name Only With Explicit Title
|
||||
> Superseded in v2.16.0 (ADR-076): the title is `notes.title`, not a `#` first line. p-name still appears only for titled notes.
|
||||
|
||||
**Per Q22**:
|
||||
- Detected by checking if content starts with `#`
|
||||
- Matches note model's title extraction logic
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
# v2.16.0: Article titles and summaries
|
||||
|
||||
Implements ADR-076. Prompted by a review of how StarPunk handles IndieWeb
|
||||
articles: titles were repeated in feeds, every feed item had a made-up title,
|
||||
Micropub `summary` was dropped, and any note starting with a heading became an
|
||||
article.
|
||||
|
||||
## What changed
|
||||
|
||||
| Area | Change |
|
||||
|---|---|
|
||||
| Storage | Migration 023 adds `notes.title` and `notes.summary`; `create_note`/`update_note` take `title` and `summary` (`UNCHANGED` sentinel on update; `None`/"" clears) |
|
||||
| Migration data step | `migrations.DATA_MIGRATIONS["023_note_titles.sql"] = _move_note_titles`; `run_migrations(..., data_dir=)` gets `DATA_PATH` from `init_db`. Moves `# Heading` first lines (via `split_title`) into `title`, finds trashed files with `utils.find_trashed_file` (shared with `undelete_note`), updates `content_hash`, empties `notes_fts`. Files are restored if the migration fails |
|
||||
| Model | `Note.title`/`summary` fields; `Note.label` for places that need some name; `has_title`, `body_html` and the first-line `title` property removed |
|
||||
| Micropub | `name` → title, `summary` → summary (only with content), `summary` updatable, `q=source` returns both; a heading repeating `name` is dropped (`drop_title_heading`) |
|
||||
| Feeds | Titles only for articles (RSS none, JSON none, Atom empty); Atom/JSON summaries |
|
||||
| Templates | Article page: `p-name`, `p-summary`, `e-content` without the title. Previews: title, summary or excerpt, "Read more", no images (hidden `u-photo` data kept for Post Type Discovery) |
|
||||
| Crossposting | `compose_text` posts articles as title + `article_teaser` (summary, or 200 characters of plain text) + link + hashtags; no images for articles; Bluesky card is the article |
|
||||
| Editor | Title and Summary fields on new/edit; content not required when titled |
|
||||
| Search | FTS indexes `title`; untitled notes still use their first line |
|
||||
| MCP | `create_note`/`update_note` take `summary`; note summaries include it |
|
||||
|
||||
## Fixes found along the way
|
||||
|
||||
- **Hashtag lines.** Python-Markdown made `#indieweb is fun` an `<h1>`; CommonMark needs a space after `#`. `markdown_ext.HashtagLineExtension` escapes the first `#` of a line where `#`s run straight into text, after fenced code and raw HTML are set aside. It is used for pages and feeds (`MARKDOWN_EXTENSIONS`) and crosspost plain text (`syndication._note_soup`), so the hashtag also reaches Bluesky as a tag. Search's first-line label keeps hashtags too.
|
||||
- **Plain-text excerpt.** `Note.excerpt` only stripped `#` marks, so previews showed `**bold**` and `[link](url)`. It is now `syndication.plain_text` of the content, cut to 200 characters with `…`; `article_teaser` is the summary or this excerpt.
|
||||
|
||||
## Choices made with the owner
|
||||
|
||||
- Title column rather than an editor-only field, as the most standards-compliant option.
|
||||
- Atom entries for notes keep an empty `<title>` (required by RFC 4287).
|
||||
- Homepage and tag pages: articles show title and summary without images.
|
||||
- Crossposts: articles as title + summary + link, no images.
|
||||
|
||||
## Tests
|
||||
|
||||
- `tests/test_articles.py` (new): feeds per format, article page mf2, homepage and tag-page previews, admin create/edit/clear, title-only article.
|
||||
- `tests/test_migration_023.py` (new): titles moved (including trashed notes), hashtag first line untouched, hashes and FTS, runs once, files restored on failure, schema-only without `data_dir`.
|
||||
- Updated for the new behaviour: `test_feed.py`, `test_feeds_{atom,json,rss}.py`, `test_routes_feed.py`, `test_models.py`, `test_microformats.py`, `test_microformats_v280.py`, `test_micropub.py`, `test_micropub_v240.py` (plus new summary and empty-body tests), `test_syndication.py` and `test_syndication_bluesky.py` (article crossposts).
|
||||
- Locally: the 35 affected test files, 872+ tests, pass. CI runs the full suite on the tag.
|
||||
@@ -1,5 +1,7 @@
|
||||
# v2.3.3: Micropub titles, q=source URL, single title display
|
||||
|
||||
> Superseded in v2.16.0 by ADR-076: titles are stored in `notes.title`, not as a `# Heading` in the content, and `split_title` is used only to move old headings and drop a heading that repeats `name`.
|
||||
|
||||
Found while building tuipub, a terminal Micropub client, against StarPunk.
|
||||
|
||||
## Problems
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
-- Migration 023: Note titles and summaries (v2.16.0, ADR-076)
|
||||
-- An article's title moves out of its Markdown ('# Heading' first line) into
|
||||
-- notes.title; migrations.py moves existing headings right after this runs.
|
||||
-- notes.summary holds an author-written summary (Micropub 'summary').
|
||||
ALTER TABLE notes ADD COLUMN title TEXT;
|
||||
ALTER TABLE notes ADD COLUMN summary TEXT;
|
||||
@@ -468,5 +468,5 @@ def create_app(config=None):
|
||||
|
||||
# Package version (Semantic Versioning 2.0.0)
|
||||
# See docs/standards/versioning-strategy.md for details
|
||||
__version__ = "2.15.0"
|
||||
__version_info__ = (2, 15, 0)
|
||||
__version__ = "2.16.0"
|
||||
__version_info__ = (2, 16, 0)
|
||||
@@ -17,10 +17,12 @@ def init_db(app=None):
|
||||
if app:
|
||||
db_path = app.config["DATABASE_PATH"]
|
||||
logger = app.logger
|
||||
data_dir = app.config.get("DATA_PATH")
|
||||
else:
|
||||
# Fallback to default path
|
||||
db_path = Path("./data/starpunk.db")
|
||||
logger = None
|
||||
data_dir = None
|
||||
|
||||
# Ensure parent directory exists
|
||||
db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
@@ -41,4 +43,4 @@ def init_db(app=None):
|
||||
|
||||
# Run migrations
|
||||
from starpunk.migrations import run_migrations
|
||||
run_migrations(db_path, logger=logger)
|
||||
run_migrations(db_path, logger=logger, data_dir=data_dir)
|
||||
@@ -161,7 +161,10 @@ def generate_atom_streaming(
|
||||
|
||||
# Required elements
|
||||
yield f' <id>{_escape_xml(permalink)}</id>\n'
|
||||
yield f' <title>{_escape_xml(note.title)}</title>\n'
|
||||
# atom:title is required but may be empty: untitled notes (ADR-076)
|
||||
yield f' <title>{_escape_xml(note.title or "")}</title>\n'
|
||||
if note.summary:
|
||||
yield f' <summary>{_escape_xml(note.summary)}</summary>\n'
|
||||
|
||||
# Use created_at for both published and updated
|
||||
# (Note model doesn't have updated_at tracking yet)
|
||||
|
||||
@@ -229,8 +229,12 @@ def _build_item_object(site_url: str, note: Note) -> Dict[str, Any]:
|
||||
"url": permalink,
|
||||
}
|
||||
|
||||
# Add title
|
||||
item["title"] = note.title
|
||||
# Title and summary only when the note has them: untitled notes have
|
||||
# no title (JSON Feed 1.1; v2.16.0, ADR-076)
|
||||
if note.title:
|
||||
item["title"] = note.title
|
||||
if note.summary:
|
||||
item["summary"] = note.summary
|
||||
|
||||
# Add image field (URL of first/main image) - per JSON Feed 1.1 spec
|
||||
# Per Q7: Field should be absent (not null) when no media
|
||||
|
||||
+14
-37
@@ -108,7 +108,9 @@ def generate_rss(
|
||||
|
||||
# Set required item elements
|
||||
fe.id(permalink)
|
||||
fe.title(get_note_title(note))
|
||||
title = get_note_title(note)
|
||||
if title: # untitled notes have only a description (RSS 2.0, ADR-076)
|
||||
fe.title(title)
|
||||
fe.link(href=permalink)
|
||||
fe.guid(permalink, permalink=True)
|
||||
|
||||
@@ -263,8 +265,8 @@ def generate_rss_streaming(
|
||||
html_content += clean_html_for_rss(note.feed_html)
|
||||
|
||||
# Build item XML
|
||||
item_xml = f""" <item>
|
||||
<title>{_escape_xml(title)}</title>
|
||||
title_xml = f"\n <title>{_escape_xml(title)}</title>" if title else ""
|
||||
item_xml = f""" <item>{title_xml}
|
||||
<link>{_escape_xml(permalink)}</link>
|
||||
<guid isPermaLink="true">{_escape_xml(permalink)}</guid>
|
||||
<pubDate>{pub_date_str}</pubDate>"""
|
||||
@@ -494,48 +496,23 @@ def format_rfc822_date(dt: datetime) -> str:
|
||||
return dt.strftime("%a, %d %b %Y %H:%M:%S %z")
|
||||
|
||||
|
||||
def get_note_title(note: Note) -> str:
|
||||
def get_note_title(note: Note) -> Optional[str]:
|
||||
"""
|
||||
Extract title from note content
|
||||
An item's title: the note's title (articles), cut to 100 characters
|
||||
|
||||
Attempts to extract a meaningful title from the note. Uses the first
|
||||
line of content (stripped of markdown heading syntax) or falls back
|
||||
to a formatted timestamp if content is unavailable.
|
||||
|
||||
Algorithm:
|
||||
1. Try note.title property (first line, stripped of # syntax)
|
||||
2. Fall back to timestamp if title is unavailable
|
||||
Untitled notes get none (v2.16.0, ADR-076), so readers can tell notes
|
||||
from articles; RSS 2.0 lets an item have just a description.
|
||||
|
||||
Args:
|
||||
note: Note object
|
||||
|
||||
Returns:
|
||||
Title string (max 100 chars, truncated if needed)
|
||||
|
||||
Examples:
|
||||
>>> # Note with heading
|
||||
>>> note = Note(...) # content: "# My First Note\\n\\n..."
|
||||
>>> get_note_title(note)
|
||||
'My First Note'
|
||||
|
||||
>>> # Note without heading (timestamp fallback)
|
||||
>>> note = Note(...) # content: "Just some text"
|
||||
>>> get_note_title(note)
|
||||
'November 18, 2024 at 12:00 PM'
|
||||
Title string, or None for an untitled note
|
||||
"""
|
||||
try:
|
||||
# Use Note's title property (handles extraction logic)
|
||||
title = note.title
|
||||
|
||||
# Truncate to 100 characters for RSS compatibility
|
||||
if len(title) > 100:
|
||||
title = title[:100].strip() + "..."
|
||||
|
||||
return title
|
||||
|
||||
except (FileNotFoundError, OSError, AttributeError):
|
||||
# If title extraction fails, use timestamp
|
||||
return note.created_at.strftime("%B %d, %Y at %I:%M %p")
|
||||
title = note.title
|
||||
if title and len(title) > 100:
|
||||
title = title[:100].strip() + "..."
|
||||
return title or None
|
||||
|
||||
|
||||
def clean_html_for_rss(html: str) -> str:
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
"""
|
||||
Markdown extension: turn bare URLs into links (v2.3.0)
|
||||
Markdown extensions for notes
|
||||
|
||||
Python-Markdown only links <https://...>; notes often contain pasted URLs.
|
||||
BareUrlExtension: turn bare URLs into links (v2.3.0). Python-Markdown only
|
||||
links <https://...>; notes often contain pasted URLs.
|
||||
|
||||
HashtagLineExtension: a line starting with a hashtag stays text (v2.16.0).
|
||||
Python-Markdown makes '#indieweb is fun' a heading; CommonMark (and every
|
||||
other Markdown a client may use) needs a space after the '#'.
|
||||
"""
|
||||
|
||||
import re
|
||||
@@ -9,6 +14,7 @@ import xml.etree.ElementTree as etree
|
||||
|
||||
from markdown.extensions import Extension
|
||||
from markdown.inlinepatterns import InlineProcessor
|
||||
from markdown.preprocessors import Preprocessor
|
||||
from markdown.treeprocessors import Treeprocessor
|
||||
from markdown.util import AtomicString
|
||||
|
||||
@@ -40,3 +46,20 @@ class BareUrlExtension(Extension):
|
||||
# Below links (160) and <autolinks> (120), so those win
|
||||
md.inlinePatterns.register(_BareUrl(BARE_URL_RE, md), "bare_url", 105)
|
||||
md.treeprocessors.register(_UnnestLinks(md), "unnest_links", 0)
|
||||
|
||||
|
||||
# Up to 3 spaces, then '#'s straight followed by text: a hashtag, not a heading
|
||||
_HASHTAG_LINE_RE = re.compile(r"^( {0,3})(?=#+[^\s#])")
|
||||
|
||||
|
||||
class _HashtagLines(Preprocessor):
|
||||
"""Escape the first '#' of such a line, so it renders as text"""
|
||||
|
||||
def run(self, lines):
|
||||
return [_HASHTAG_LINE_RE.sub(r"\1\\", line) for line in lines]
|
||||
|
||||
|
||||
class HashtagLineExtension(Extension):
|
||||
def extendMarkdown(self, md):
|
||||
# After fenced code (25) and raw HTML (20) are set aside, so code is untouched
|
||||
md.preprocessors.register(_HashtagLines(md), "hashtag_lines", 15)
|
||||
@@ -73,6 +73,7 @@ def _summary(note) -> dict:
|
||||
"url": _note_url(note.slug),
|
||||
"slug": note.slug,
|
||||
"title": note.title,
|
||||
"summary": note.summary,
|
||||
"kind": note.kind,
|
||||
"status": "published" if note.published else "draft",
|
||||
"published": iso_utc(note.created_at),
|
||||
@@ -217,6 +218,7 @@ def register_tools(server: MCPServer, app: Flask) -> None:
|
||||
async def create_note(
|
||||
content: str = "",
|
||||
title: Optional[str] = None,
|
||||
summary: Optional[str] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
draft: bool = True,
|
||||
photos: Optional[list[Photo]] = None,
|
||||
@@ -230,7 +232,8 @@ def register_tools(server: MCPServer, app: Flask) -> None:
|
||||
) -> dict:
|
||||
"""Create a note (Markdown). It is a draft unless draft is false;
|
||||
publishing makes it public and crossposts it to the syndicate_to
|
||||
targets. A title makes it an article. At most one of like_of,
|
||||
targets. A title makes it an article; summary is its short summary,
|
||||
shown on the homepage and in feeds and crossposts. At most one of like_of,
|
||||
repost_of and bookmark_of. published_at is an ISO 8601 date."""
|
||||
info = _token_info()
|
||||
_require(info, "create", "draft")
|
||||
@@ -240,6 +243,8 @@ def register_tools(server: MCPServer, app: Flask) -> None:
|
||||
props["content"] = [content]
|
||||
if title:
|
||||
props["name"] = [title]
|
||||
if summary:
|
||||
props["summary"] = [summary]
|
||||
if tags:
|
||||
props["category"] = list(tags)
|
||||
if photos:
|
||||
@@ -268,6 +273,7 @@ def register_tools(server: MCPServer, app: Flask) -> None:
|
||||
url: str,
|
||||
content: Optional[str] = None,
|
||||
title: Optional[str] = None,
|
||||
summary: Optional[str] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
add_tags: Optional[list[str]] = None,
|
||||
remove_tags: Optional[list[str]] = None,
|
||||
@@ -276,7 +282,7 @@ def register_tools(server: MCPServer, app: Flask) -> None:
|
||||
in_reply_to: Optional[str] = None,
|
||||
) -> dict:
|
||||
"""Change a note. Only the fields given change: content replaces the
|
||||
text, title replaces the title ("" removes it), tags replaces all
|
||||
text, title replaces the title ("" removes it), summary likewise, tags replaces all
|
||||
tags, add_tags/remove_tags adjust them, photos replaces all photos,
|
||||
draft false publishes a draft (and crossposts it) and true unpublishes.
|
||||
Edits to published notes follow to their crossposts."""
|
||||
@@ -288,6 +294,8 @@ def register_tools(server: MCPServer, app: Flask) -> None:
|
||||
replace["content"] = [content]
|
||||
if title is not None:
|
||||
replace["name"] = [title]
|
||||
if summary is not None:
|
||||
replace["summary"] = [summary]
|
||||
if tags is not None:
|
||||
replace["category"] = list(tags)
|
||||
if draft is not None:
|
||||
|
||||
+49
-26
@@ -7,7 +7,8 @@ interface for creating posts via external clients.
|
||||
Functions:
|
||||
normalize_properties: Convert form/JSON data to Micropub properties format
|
||||
extract_content: Get content from Micropub properties
|
||||
extract_title: Get the explicit title (name) from Micropub properties
|
||||
extract_title: Get the title (name) from Micropub properties
|
||||
extract_summary: Get the author's summary from Micropub properties
|
||||
extract_tags: Get category tags from Micropub properties
|
||||
handle_create: Process Micropub create action
|
||||
handle_update: Process Micropub update action (v2.4.0)
|
||||
@@ -235,10 +236,7 @@ def _has_text(content) -> bool:
|
||||
|
||||
def extract_title(properties: dict) -> Optional[str]:
|
||||
"""
|
||||
Extract the explicit title (Micropub 'name' property)
|
||||
|
||||
StarPunk has no title field: a note's title is a '# Heading' first line
|
||||
(see Note.title), so the caller stores the name as that heading.
|
||||
Extract the title (Micropub 'name' property), stored in notes.title (ADR-076)
|
||||
|
||||
Args:
|
||||
properties: Normalized Micropub properties dict
|
||||
@@ -252,14 +250,22 @@ def extract_title(properties: dict) -> Optional[str]:
|
||||
return " ".join(name.split()) or None
|
||||
|
||||
|
||||
def add_title_heading(content: str, title: Optional[str]) -> str:
|
||||
"""Prefix content with '# title' unless it already starts with that heading"""
|
||||
if not title:
|
||||
return content
|
||||
first_line = content.lstrip().split("\n", 1)[0]
|
||||
if first_line.startswith("#") and first_line.lstrip("#").strip() == title:
|
||||
return content
|
||||
return f"# {title}\n\n{content}"
|
||||
def extract_summary(properties: dict) -> Optional[str]:
|
||||
"""The author's summary (Micropub 'summary' property, v2.16.0), as plain text"""
|
||||
summary = _first(properties.get("summary"))
|
||||
if isinstance(summary, dict):
|
||||
summary = summary.get("text") or summary.get("value") or ""
|
||||
if not isinstance(summary, str):
|
||||
return None
|
||||
return summary.strip() or None
|
||||
|
||||
|
||||
def drop_title_heading(content: str, title: Optional[str]) -> str:
|
||||
"""Content without a '# heading' first line that only repeats the title"""
|
||||
heading, rest = split_title(content)
|
||||
if title and heading == title:
|
||||
return rest
|
||||
return content
|
||||
|
||||
|
||||
def extract_tags(properties: dict) -> list[str]:
|
||||
@@ -308,7 +314,7 @@ MAX_PHOTOS = 4 # ADR-057
|
||||
MAX_REMOTE_PHOTO_BYTES = 20 * 1024 * 1024
|
||||
|
||||
# Properties we can store, and so can be updated (v2.4.0)
|
||||
UPDATABLE = {"content", "name", "category", "in-reply-to", "post-status", "photo"}
|
||||
UPDATABLE = {"content", "name", "summary", "category", "in-reply-to", "post-status", "photo"}
|
||||
|
||||
|
||||
def _first(value):
|
||||
@@ -596,6 +602,10 @@ def handle_create(data: dict, token_info: dict, files=None):
|
||||
if has_content or not (like_of or repost_of or bookmark_of or photos or title)
|
||||
else ""
|
||||
)
|
||||
content = drop_title_heading(content, title)
|
||||
# A summary with content is the author's summary; alone it is the
|
||||
# content (Post Type Discovery), as extract_content does (v2.16.0)
|
||||
summary = extract_summary(properties) if _has_text(properties.get("content")) else None
|
||||
tags = extract_tags(properties)
|
||||
published_date = extract_published_date(properties)
|
||||
published = extract_post_status(properties) and not extract_visibility_draft(properties)
|
||||
@@ -615,7 +625,9 @@ def handle_create(data: dict, token_info: dict, files=None):
|
||||
note = None
|
||||
try:
|
||||
note = create_note(
|
||||
content=add_title_heading(content, title),
|
||||
content=content,
|
||||
title=title,
|
||||
summary=summary,
|
||||
published=published,
|
||||
created_at=published_date,
|
||||
custom_slug=custom_slug,
|
||||
@@ -686,7 +698,7 @@ def handle_update(data: dict, token_info: dict):
|
||||
|
||||
from starpunk.tags import get_note_tags
|
||||
|
||||
title, body = split_title(note.content)
|
||||
title, summary, body = note.title, note.summary, note.content
|
||||
tags = [t["display_name"] for t in get_note_tags(note.id, entered_order=True)]
|
||||
photos = _note_photos(note)
|
||||
changes = {}
|
||||
@@ -696,6 +708,8 @@ def handle_update(data: dict, token_info: dict):
|
||||
for prop in delete:
|
||||
if prop == "name":
|
||||
title = None
|
||||
elif prop == "summary":
|
||||
summary = None
|
||||
elif prop == "content":
|
||||
body = ""
|
||||
elif prop == "category":
|
||||
@@ -723,6 +737,8 @@ def handle_update(data: dict, token_info: dict):
|
||||
changes["content_format"] = content_format(replace) if body else None
|
||||
if "name" in replace:
|
||||
title = extract_title(replace)
|
||||
if "summary" in replace:
|
||||
summary = extract_summary(replace)
|
||||
if "category" in replace:
|
||||
tags = extract_tags(replace)
|
||||
if "photo" in replace:
|
||||
@@ -738,17 +754,22 @@ def handle_update(data: dict, token_info: dict):
|
||||
if "photo" in add:
|
||||
photos += extract_photos(add)
|
||||
|
||||
# Text may go only if photos stay (or it's a like, repost or bookmark) (v2.6.5)
|
||||
if not (body or "").strip() and not (photos or note.reacts_to):
|
||||
raise MicropubValidationError("A post needs text or a photo")
|
||||
# Text may go only if photos stay, it has a title, or it's a like, repost
|
||||
# or bookmark (v2.6.5)
|
||||
if not (body or "").strip() and not (photos or title or note.reacts_to):
|
||||
raise MicropubValidationError("A post needs text, a title or a photo")
|
||||
|
||||
# Photos must all resolve before anything changes (v2.9.0)
|
||||
if "photo" in touched:
|
||||
media_ids, captions = _resolve_photos(photos)
|
||||
|
||||
new_content = add_title_heading(body, title)
|
||||
if new_content != note.content:
|
||||
changes["content"] = new_content
|
||||
if title != note.title:
|
||||
changes["title"] = title
|
||||
if summary != note.summary:
|
||||
changes["summary"] = summary
|
||||
body = drop_title_heading(body, title)
|
||||
if body != note.content:
|
||||
changes["content"] = body
|
||||
elif "content_format" in changes and changes["content_format"] == note.content_format:
|
||||
del changes["content_format"]
|
||||
if {"category"} & touched:
|
||||
@@ -800,11 +821,11 @@ def note_to_mf2(note, wanted: Optional[list] = None) -> dict:
|
||||
"""
|
||||
A note as Microformats2 JSON, the inverse of create
|
||||
|
||||
A '# Heading' first line is the title, returned as name.
|
||||
The title is returned as name and the author's summary as summary.
|
||||
"""
|
||||
from starpunk.syndication import get_note_syndications
|
||||
|
||||
title, content = split_title(note.content)
|
||||
content = note.content
|
||||
props = {
|
||||
# HTML-authored content comes back as HTML (3.7.2, v2.9.0)
|
||||
"content": [{"html": content} if note.content_format == "html" else content],
|
||||
@@ -812,8 +833,10 @@ def note_to_mf2(note, wanted: Optional[list] = None) -> dict:
|
||||
"url": [absolute_url_for("public.note", slug=note.slug)],
|
||||
"post-status": ["published" if note.published else "draft"],
|
||||
}
|
||||
if title:
|
||||
props["name"] = [title]
|
||||
if note.title:
|
||||
props["name"] = [note.title]
|
||||
if note.summary:
|
||||
props["summary"] = [note.summary]
|
||||
from starpunk.tags import get_note_tags
|
||||
|
||||
tags = get_note_tags(note.id, entered_order=True)
|
||||
|
||||
+67
-1
@@ -344,7 +344,65 @@ def apply_migration(conn, migration_name, migration_path, logger=None):
|
||||
raise MigrationError(error_msg)
|
||||
|
||||
|
||||
def run_migrations(db_path, logger=None):
|
||||
def _move_note_titles(conn, data_dir, logger):
|
||||
"""
|
||||
Migration 023 data step: move each note's '# Heading' title into notes.title
|
||||
|
||||
Runs once, inside the migration transaction. Rewrites the note files without
|
||||
the heading and returns [(path, original text)] so a failed migration can
|
||||
put them back (see _restore_files).
|
||||
"""
|
||||
from starpunk.models import split_title
|
||||
from starpunk.utils import calculate_content_hash, find_trashed_file, write_note_file
|
||||
|
||||
data_dir = Path(data_dir)
|
||||
written = []
|
||||
try:
|
||||
rows = conn.execute(
|
||||
"SELECT id, slug, file_path FROM notes WHERE title IS NULL"
|
||||
).fetchall()
|
||||
for note_id, slug, file_path in rows:
|
||||
path = data_dir / file_path
|
||||
if not path.exists():
|
||||
path = find_trashed_file(data_dir, file_path)
|
||||
if path is None:
|
||||
logger.warning(f"Migration 023: no file for note {slug}, title not moved")
|
||||
continue
|
||||
text = path.read_text(encoding="utf-8")
|
||||
title, body = split_title(text)
|
||||
if title is None:
|
||||
continue
|
||||
conn.execute(
|
||||
"UPDATE notes SET title = ?, content_hash = ? WHERE id = ?",
|
||||
(title, calculate_content_hash(body), note_id),
|
||||
)
|
||||
write_note_file(path, body)
|
||||
written.append((path, text))
|
||||
if written and table_exists(conn, "notes_fts"):
|
||||
# Emptied so startup rebuilds it with the titles (starpunk/__init__.py)
|
||||
conn.execute("DELETE FROM notes_fts")
|
||||
logger.info(f"Migration 023: moved {len(written)} titles out of note files")
|
||||
return written
|
||||
except Exception:
|
||||
_restore_files(written, logger)
|
||||
raise
|
||||
|
||||
|
||||
def _restore_files(written, logger):
|
||||
"""Put back note files changed by a data step whose migration failed"""
|
||||
for path, text in written:
|
||||
try:
|
||||
path.write_text(text, encoding="utf-8")
|
||||
except OSError as e:
|
||||
logger.critical(f"Could not restore {path} after failed migration: {e}")
|
||||
|
||||
|
||||
# Python steps run right after a migration's SQL, in the same transaction.
|
||||
# Each returns [(path, original text)] for files it changed.
|
||||
DATA_MIGRATIONS = {"023_note_titles.sql": _move_note_titles}
|
||||
|
||||
|
||||
def run_migrations(db_path, logger=None, data_dir=None):
|
||||
"""
|
||||
Run all pending database migrations with concurrency protection
|
||||
|
||||
@@ -374,6 +432,8 @@ def run_migrations(db_path, logger=None):
|
||||
Args:
|
||||
db_path: Path to SQLite database file
|
||||
logger: Optional logger for output
|
||||
data_dir: Data directory, for migrations that also change note files
|
||||
(DATA_MIGRATIONS); skipped when None
|
||||
|
||||
Raises:
|
||||
MigrationError: If any migration fails to apply or lock cannot be acquired
|
||||
@@ -407,6 +467,7 @@ def run_migrations(db_path, logger=None):
|
||||
# BEGIN IMMEDIATE acquires RESERVED lock, preventing other writes
|
||||
# but allowing reads. Escalates to EXCLUSIVE during actual writes.
|
||||
conn.execute("BEGIN IMMEDIATE")
|
||||
written = [] # note files changed by DATA_MIGRATIONS, restored on failure
|
||||
|
||||
try:
|
||||
# Ensure migrations tracking table exists
|
||||
@@ -510,6 +571,9 @@ def run_migrations(db_path, logger=None):
|
||||
|
||||
# Execute migration inside the locked transaction
|
||||
execute_in_transaction(conn, migration_sql)
|
||||
data_step = DATA_MIGRATIONS.get(migration_name)
|
||||
if data_step and data_dir is not None:
|
||||
written += data_step(conn, data_dir, logger)
|
||||
|
||||
# Record migration as applied
|
||||
conn.execute(
|
||||
@@ -547,6 +611,7 @@ def run_migrations(db_path, logger=None):
|
||||
|
||||
except MigrationError:
|
||||
# Migration error - rollback and re-raise
|
||||
_restore_files(written, logger)
|
||||
try:
|
||||
conn.rollback()
|
||||
except Exception as rollback_error:
|
||||
@@ -556,6 +621,7 @@ def run_migrations(db_path, logger=None):
|
||||
|
||||
except Exception as e:
|
||||
# Unexpected error during migration - rollback and wrap
|
||||
_restore_files(written, logger)
|
||||
try:
|
||||
conn.rollback()
|
||||
except Exception as rollback_error:
|
||||
|
||||
+28
-104
@@ -36,9 +36,9 @@ DEFAULT_AUTH_STATE_EXPIRY_MINUTES = 5
|
||||
DEFAULT_TOKEN_EXPIRY_DAYS = 90
|
||||
|
||||
# Constants - Markdown rendering
|
||||
from starpunk.markdown_ext import BareUrlExtension
|
||||
from starpunk.markdown_ext import BareUrlExtension, HashtagLineExtension
|
||||
|
||||
MARKDOWN_EXTENSIONS = ["extra", "codehilite", "nl2br", BareUrlExtension()]
|
||||
MARKDOWN_EXTENSIONS = ["extra", "codehilite", "nl2br", BareUrlExtension(), HashtagLineExtension()]
|
||||
|
||||
# Constants - Content limits
|
||||
MAX_TITLE_LENGTH = 200
|
||||
@@ -76,11 +76,14 @@ EXCERPT_LENGTH = 200
|
||||
|
||||
def split_title(content: str) -> tuple[Optional[str], str]:
|
||||
"""
|
||||
Split a note's '# Heading' first line (its title, per Q22) off its content
|
||||
Split a '# Heading' first line off Markdown content
|
||||
|
||||
Before v2.16.0 an article's title was its '# Heading' first line (Q22).
|
||||
Titles now live in notes.title (ADR-076); migration 023 uses this to move
|
||||
the old headings there, and Micropub to drop a heading that repeats 'name'.
|
||||
|
||||
The first line must be a Markdown heading: 1-6 '#' then a space and text.
|
||||
'#indieweb ...' (a hashtag) or a bare '#' is not a title. This is the one
|
||||
place that decides whether a note has a title (templates use has_title).
|
||||
'#indieweb ...' (a hashtag) or a bare '#' is not a title.
|
||||
|
||||
Returns:
|
||||
(title, remaining content); title is None if the note has no heading
|
||||
@@ -171,6 +174,8 @@ class Note:
|
||||
repost_of: Optional[str] = None # v2.5.0: URL reposted (this note is a repost)
|
||||
bookmark_of: Optional[str] = None # v2.9.0: URL bookmarked (this note is a bookmark)
|
||||
content_format: Optional[str] = None # v2.9.0: 'html' if sent as HTML via Micropub
|
||||
title: Optional[str] = None # v2.16.0: an article's title (p-name), ADR-076
|
||||
summary: Optional[str] = None # v2.16.0: author-written summary (p-summary)
|
||||
_cached_content: Optional[str] = field(
|
||||
default=None, repr=False, compare=False, init=False
|
||||
)
|
||||
@@ -233,6 +238,8 @@ class Note:
|
||||
repost_of=data.get("repost_of"),
|
||||
bookmark_of=data.get("bookmark_of"),
|
||||
content_format=data.get("content_format"),
|
||||
title=data.get("title"),
|
||||
summary=data.get("summary"),
|
||||
)
|
||||
|
||||
@property
|
||||
@@ -300,124 +307,40 @@ class Note:
|
||||
return self._cached_html
|
||||
|
||||
@property
|
||||
def body_html(self) -> str:
|
||||
"""
|
||||
Rendered HTML without the title heading
|
||||
|
||||
Pages show a titled note's title as their own heading, so the body
|
||||
leaves it out to avoid showing it twice. Untitled notes: same as html.
|
||||
"""
|
||||
title, body = split_title(self.content)
|
||||
if title is None:
|
||||
return self.html
|
||||
return render_markdown(body)
|
||||
|
||||
@property
|
||||
def has_title(self) -> bool:
|
||||
"""True if the note starts with a '# Heading' title (an article)"""
|
||||
try:
|
||||
return split_title(self.content)[0] is not None
|
||||
except (FileNotFoundError, OSError):
|
||||
return False
|
||||
|
||||
@property
|
||||
def title(self) -> str:
|
||||
"""
|
||||
Extract title from content
|
||||
|
||||
Returns first line of content, or uses slug as fallback.
|
||||
Strips markdown heading syntax (# ) if present.
|
||||
|
||||
Returns:
|
||||
Title string
|
||||
|
||||
Examples:
|
||||
>>> # Content: "# My First Note\\n\\nContent here..."
|
||||
>>> note.title
|
||||
'My First Note'
|
||||
|
||||
>>> # Content: "Just a note without heading"
|
||||
>>> note.title
|
||||
'Just a note without heading'
|
||||
"""
|
||||
try:
|
||||
# Get content (may trigger lazy load)
|
||||
content = self.content
|
||||
|
||||
# Split into lines and get first non-empty line
|
||||
lines = content.strip().split("\n")
|
||||
first_line = ""
|
||||
for line in lines:
|
||||
if line.strip():
|
||||
first_line = line.strip()
|
||||
break
|
||||
|
||||
if not first_line:
|
||||
# No content, use slug
|
||||
return self.slug
|
||||
|
||||
# Strip markdown heading syntax
|
||||
if first_line.startswith("#"):
|
||||
# Remove leading # characters and whitespace
|
||||
first_line = first_line.lstrip("#").strip()
|
||||
|
||||
# Truncate to max length
|
||||
if len(first_line) > MAX_TITLE_LENGTH:
|
||||
first_line = first_line[:MAX_TITLE_LENGTH]
|
||||
|
||||
return first_line if first_line else self.slug
|
||||
|
||||
except (FileNotFoundError, OSError):
|
||||
# If file doesn't exist, use slug
|
||||
return self.slug
|
||||
def label(self) -> str:
|
||||
"""A name for places that always need one: the title, else the excerpt, else the slug"""
|
||||
return self.title or " ".join(self.excerpt.split())[:MAX_TITLE_LENGTH] or self.slug
|
||||
|
||||
@property
|
||||
def excerpt(self) -> str:
|
||||
"""
|
||||
Generate short excerpt for previews
|
||||
|
||||
Returns first 200 characters of content (plain text, no markdown).
|
||||
Strips markdown formatting and adds ellipsis if truncated.
|
||||
The first 200 characters of the note as plain text: Markdown (or
|
||||
HTML) rendered and stripped, images left out, links as their text
|
||||
(v2.16.0). Cut at a word boundary with an ellipsis if longer.
|
||||
|
||||
Returns:
|
||||
Excerpt string
|
||||
|
||||
Examples:
|
||||
>>> note.excerpt
|
||||
'This is my note content. It has some interesting points...'
|
||||
'This is my note content. It has some interesting points…'
|
||||
"""
|
||||
from starpunk.syndication import plain_text
|
||||
|
||||
try:
|
||||
# Leave out the title; previews show it separately
|
||||
_, content = split_title(self.content)
|
||||
|
||||
# Remove markdown heading syntax
|
||||
lines = content.split("\n")
|
||||
cleaned_lines = []
|
||||
for line in lines:
|
||||
if line.strip().startswith("#"):
|
||||
# Remove heading syntax
|
||||
cleaned_lines.append(line.lstrip("#").strip())
|
||||
else:
|
||||
cleaned_lines.append(line)
|
||||
|
||||
cleaned_content = "\n".join(cleaned_lines)
|
||||
|
||||
# Take first EXCERPT_LENGTH characters
|
||||
excerpt = cleaned_content.strip()[:EXCERPT_LENGTH]
|
||||
|
||||
# Truncate to word boundary
|
||||
if len(cleaned_content.strip()) > EXCERPT_LENGTH:
|
||||
# Find last space in excerpt
|
||||
text = " ".join(plain_text(self.content).split())
|
||||
if len(text) > EXCERPT_LENGTH:
|
||||
excerpt = text[:EXCERPT_LENGTH]
|
||||
last_space = excerpt.rfind(" ")
|
||||
if last_space > 0:
|
||||
excerpt = excerpt[:last_space]
|
||||
excerpt += "..."
|
||||
text = (excerpt[:last_space] if last_space > 0 else excerpt).rstrip() + "…"
|
||||
|
||||
if not excerpt and self.reacts_to:
|
||||
if not text and self.reacts_to:
|
||||
# A like, repost or bookmark with no comment
|
||||
return f"{REACTION_VERBS[self.kind]} {self.reacts_to}"
|
||||
|
||||
return excerpt
|
||||
return text
|
||||
|
||||
except (FileNotFoundError, OSError):
|
||||
# If file doesn't exist, return slug
|
||||
@@ -533,6 +456,7 @@ class Note:
|
||||
"id": self.id,
|
||||
"slug": self.slug,
|
||||
"title": self.title,
|
||||
"summary": self.summary,
|
||||
"published": self.published,
|
||||
"created_at": self.created_at.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
"updated_at": self.updated_at.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
|
||||
+41
-21
@@ -149,6 +149,8 @@ def create_note(
|
||||
has_media: bool = False,
|
||||
bookmark_of: Optional[str] = None,
|
||||
content_format: Optional[str] = None,
|
||||
title: Optional[str] = None,
|
||||
summary: Optional[str] = None,
|
||||
) -> Note:
|
||||
"""
|
||||
Create a new note
|
||||
@@ -164,6 +166,8 @@ def create_note(
|
||||
custom_slug: Optional custom slug (from Micropub mp-slug property)
|
||||
tags: Optional list of tag display names (v1.3.0)
|
||||
in_reply_to: Optional URL being replied to (v1.6.0)
|
||||
title: Optional title; a titled note is an article (v2.16.0, ADR-076)
|
||||
summary: Optional author-written summary (v2.16.0)
|
||||
|
||||
Returns:
|
||||
Note object with all metadata and content loaded
|
||||
@@ -176,7 +180,7 @@ def create_note(
|
||||
|
||||
Examples:
|
||||
>>> # Create unpublished draft
|
||||
>>> note = create_note("# My First Note\\n\\nContent here.", published=False)
|
||||
>>> note = create_note("Content here.", title="My First Note", published=False)
|
||||
>>> print(note.slug)
|
||||
'my-first-note'
|
||||
|
||||
@@ -219,7 +223,9 @@ def create_note(
|
||||
"like_of", like_of, "A note can only be one of a like, repost or bookmark"
|
||||
)
|
||||
content = content or ""
|
||||
if not content.strip() and not (like_of or repost_of or bookmark_of or has_media):
|
||||
title = (title or "").strip() or None
|
||||
summary = (summary or "").strip() or None
|
||||
if not content.strip() and not (like_of or repost_of or bookmark_of or has_media or title):
|
||||
raise InvalidNoteDataError(
|
||||
"content", content, "Content cannot be empty or whitespace-only"
|
||||
)
|
||||
@@ -281,11 +287,12 @@ def create_note(
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO notes (slug, file_path, published, created_at, updated_at, content_hash,
|
||||
in_reply_to, like_of, repost_of, bookmark_of, content_format)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
in_reply_to, like_of, repost_of, bookmark_of, content_format,
|
||||
title, summary)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(slug, file_path_rel, published, created_at, updated_at, content_hash,
|
||||
in_reply_to, like_of, repost_of, bookmark_of, content_format),
|
||||
in_reply_to, like_of, repost_of, bookmark_of, content_format, title, summary),
|
||||
)
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
@@ -327,7 +334,7 @@ def create_note(
|
||||
from starpunk.search import update_fts_index, has_fts_table
|
||||
db_path = Path(current_app.config["DATABASE_PATH"])
|
||||
if has_fts_table(db_path):
|
||||
update_fts_index(db, note_id, slug, content)
|
||||
update_fts_index(db, note_id, slug, content, title)
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
# FTS update failure should not prevent note creation
|
||||
@@ -641,11 +648,14 @@ def update_note(
|
||||
in_reply_to: Optional[str] = None,
|
||||
has_media: bool = False,
|
||||
content_format=UNCHANGED,
|
||||
title=UNCHANGED,
|
||||
summary=UNCHANGED,
|
||||
) -> Note:
|
||||
"""
|
||||
Update a note's content and/or published status
|
||||
|
||||
content_format (v2.9.0): 'html' or None (Markdown); left alone unless given
|
||||
title, summary (v2.16.0): new value, None or "" to remove; left alone unless given
|
||||
|
||||
Updates note content and/or metadata, maintaining atomic synchronization
|
||||
between file and database. At least one of content or published must
|
||||
@@ -713,8 +723,15 @@ def update_note(
|
||||
if slug is not None and id is not None:
|
||||
raise ValueError("Cannot provide both slug and id")
|
||||
|
||||
if content is None and published is None and tags is None and in_reply_to is None:
|
||||
raise ValueError("Must provide at least one of content, published, tags, or in_reply_to to update")
|
||||
if (content is None and published is None and tags is None and in_reply_to is None
|
||||
and title is UNCHANGED and summary is UNCHANGED):
|
||||
raise ValueError(
|
||||
"Must provide at least one of content, published, tags, in_reply_to, title or summary to update"
|
||||
)
|
||||
if title is not UNCHANGED:
|
||||
title = (title or "").strip() or None
|
||||
if summary is not UNCHANGED:
|
||||
summary = (summary or "").strip() or None
|
||||
|
||||
# Validate content if provided (checked against the note below for
|
||||
# likes/reposts, whose comment may be empty)
|
||||
@@ -729,7 +746,8 @@ def update_note(
|
||||
raise NoteNotFoundError(identifier)
|
||||
old_tag_names = _tag_names(existing_note.id) # for WebSub tag topics (v2.10.0)
|
||||
|
||||
if empty_content and not (existing_note.reacts_to or has_media):
|
||||
new_title = existing_note.title if title is UNCHANGED else title
|
||||
if empty_content and not (existing_note.reacts_to or has_media or new_title):
|
||||
raise InvalidNoteDataError(
|
||||
"content", content, "Content cannot be empty or whitespace-only"
|
||||
)
|
||||
@@ -797,6 +815,14 @@ def update_note(
|
||||
update_fields.append("content_format = ?")
|
||||
params.append(content_format)
|
||||
|
||||
if title is not UNCHANGED:
|
||||
update_fields.append("title = ?")
|
||||
params.append(title)
|
||||
|
||||
if summary is not UNCHANGED:
|
||||
update_fields.append("summary = ?")
|
||||
params.append(summary)
|
||||
|
||||
# Add WHERE clause parameter
|
||||
if slug is not None:
|
||||
where_clause = "slug = ?"
|
||||
@@ -832,13 +858,14 @@ def update_note(
|
||||
# Tag update failure should not prevent note update
|
||||
current_app.logger.warning(f"Failed to update tags for note {existing_note.slug}: {e}")
|
||||
|
||||
# 7. UPDATE FTS INDEX (if available and content changed)
|
||||
if content is not None:
|
||||
# 7. UPDATE FTS INDEX (if available and content or title changed)
|
||||
if content is not None or title is not UNCHANGED:
|
||||
try:
|
||||
from starpunk.search import update_fts_index, has_fts_table
|
||||
db_path = Path(current_app.config["DATABASE_PATH"])
|
||||
if has_fts_table(db_path):
|
||||
update_fts_index(db, existing_note.id, existing_note.slug, content)
|
||||
update_fts_index(db, existing_note.id, existing_note.slug,
|
||||
existing_note.content if content is None else content, new_title)
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
# FTS update failure should not prevent note update
|
||||
@@ -1129,7 +1156,7 @@ def undelete_note(slug: str) -> Note:
|
||||
NoteNotFoundError: If there is no soft-deleted note with this slug
|
||||
"""
|
||||
import shutil
|
||||
from starpunk.utils import TRASH_DIR_NAME
|
||||
from starpunk.utils import find_trashed_file
|
||||
|
||||
db = get_db(current_app)
|
||||
row = db.execute(
|
||||
@@ -1142,14 +1169,7 @@ def undelete_note(slug: str) -> Note:
|
||||
data_dir = Path(current_app.config["DATA_PATH"])
|
||||
note_path = data_dir / row["file_path"]
|
||||
if not note_path.exists():
|
||||
# delete_note_file put it in .trash/<year>/<month>/<name>
|
||||
parts = Path(row["file_path"]).parts
|
||||
candidates = []
|
||||
if "notes" in parts and len(parts) > parts.index("notes") + 2:
|
||||
i = parts.index("notes")
|
||||
candidates.append(data_dir / TRASH_DIR_NAME / parts[i + 1] / parts[i + 2] / note_path.name)
|
||||
candidates += sorted((data_dir / TRASH_DIR_NAME).glob(f"*/*/{note_path.name}"))
|
||||
source = next((c for c in candidates if c.exists()), None)
|
||||
source = find_trashed_file(data_dir, row["file_path"])
|
||||
if source is None:
|
||||
raise NoteSyncError("undelete", f"Trashed file not found for {slug}", "Cannot restore note file")
|
||||
note_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@@ -142,7 +142,7 @@ def _note_label(conn, note_id: int) -> str:
|
||||
|
||||
try:
|
||||
note = get_note(id=note_id, load_content=True)
|
||||
label = " ".join((note.title or "").split()) if note else ""
|
||||
label = note.label if note else ""
|
||||
except Exception:
|
||||
label = ""
|
||||
return (label[:57] + "…") if len(label) > 60 else (label or "a note")
|
||||
|
||||
@@ -128,6 +128,7 @@ def create_note_submit():
|
||||
media_files: Multiple file upload (v1.2.0 Phase 3)
|
||||
captions[]: Captions for each media file (v1.2.0 Phase 3)
|
||||
tags: Comma-separated tag list (v1.3.0 Phase 3)
|
||||
title, summary: Optional; a title makes it an article (v2.16.0)
|
||||
|
||||
Returns:
|
||||
Redirect to dashboard on success, back to form on error
|
||||
@@ -141,17 +142,19 @@ def create_note_submit():
|
||||
published = "published" in request.form
|
||||
custom_slug = request.form.get("custom_slug", "").strip()
|
||||
tags_input = request.form.get("tags", "")
|
||||
title = request.form.get("title", "").strip() or None
|
||||
summary = request.form.get("summary", "").strip() or None
|
||||
|
||||
if not content:
|
||||
flash("Content cannot be empty", "error")
|
||||
# With photos, WebSub hears about the note once they're attached (v2.10.1)
|
||||
has_uploads = any(f.filename for f in request.files.getlist('media_files'))
|
||||
|
||||
if not (content or title or has_uploads):
|
||||
flash("A note needs text, a title or an image", "error")
|
||||
return redirect(url_for("admin.new_note_form"))
|
||||
|
||||
# Parse tags (v1.3.0 Phase 3)
|
||||
tags = parse_tag_input(tags_input)
|
||||
|
||||
# With photos, WebSub hears about the note once they're attached (v2.10.1)
|
||||
has_uploads = any(f.filename for f in request.files.getlist('media_files'))
|
||||
|
||||
try:
|
||||
# Create note first (per Q4)
|
||||
note = create_note(
|
||||
@@ -160,6 +163,8 @@ def create_note_submit():
|
||||
custom_slug=custom_slug if custom_slug else None,
|
||||
tags=tags if tags else None,
|
||||
has_media=has_uploads,
|
||||
title=title,
|
||||
summary=summary,
|
||||
)
|
||||
|
||||
# Handle media uploads (v1.2.0 Phase 3)
|
||||
@@ -279,12 +284,14 @@ def update_note_submit(note_id: int):
|
||||
content: Updated markdown content (required)
|
||||
published: Checkbox for published status (optional)
|
||||
tags: Comma-separated tag list (v1.3.0 Phase 3)
|
||||
title, summary: Empty removes them; a form without the field leaves them (v2.16.0)
|
||||
|
||||
Returns:
|
||||
Redirect to dashboard on success, back to form on error
|
||||
|
||||
Decorator: @require_auth
|
||||
"""
|
||||
from starpunk.notes import UNCHANGED
|
||||
from starpunk.tags import parse_tag_input
|
||||
|
||||
# Check if note exists first
|
||||
@@ -296,8 +303,12 @@ def update_note_submit(note_id: int):
|
||||
content = request.form.get("content", "").strip()
|
||||
published = "published" in request.form
|
||||
tags_input = request.form.get("tags", "")
|
||||
title = request.form["title"].strip() if "title" in request.form else UNCHANGED
|
||||
summary = request.form["summary"].strip() if "summary" in request.form else UNCHANGED
|
||||
has_title = existing_note.title if title is UNCHANGED else title
|
||||
|
||||
if not content and not existing_note.reacts_to: # likes/reposts may have no comment
|
||||
# Likes/reposts may have no comment, articles no body
|
||||
if not content and not (existing_note.reacts_to or has_title):
|
||||
flash("Content cannot be empty", "error")
|
||||
return redirect(url_for("admin.edit_note_form", note_id=note_id))
|
||||
|
||||
@@ -311,7 +322,9 @@ def update_note_submit(note_id: int):
|
||||
published=published,
|
||||
# An empty Tags field removes all tags (v2.14.1); a form without
|
||||
# the field leaves them alone
|
||||
tags=tags if "tags" in request.form else None
|
||||
tags=tags if "tags" in request.form else None,
|
||||
title=title,
|
||||
summary=summary,
|
||||
)
|
||||
if "syndication_form" in request.form:
|
||||
from starpunk.syndication import select_targets
|
||||
|
||||
+20
-20
@@ -98,29 +98,32 @@ def has_fts_table(db_path: Path) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def update_fts_index(conn: sqlite3.Connection, note_id: int, slug: str, content: str):
|
||||
def _first_line(content: str) -> str:
|
||||
"""A note's first line without heading marks (result label for untitled notes).
|
||||
A hashtag ('#indieweb ...') is kept."""
|
||||
line = content.split('\n', 1)[0].strip()
|
||||
return re.sub(r'^#{1,6}\s+', '', line)
|
||||
|
||||
|
||||
def update_fts_index(conn: sqlite3.Connection, note_id: int, slug: str, content: str,
|
||||
title: Optional[str] = None):
|
||||
"""
|
||||
Update FTS index for a note (insert or replace)
|
||||
|
||||
Extracts title from first line of content and updates the FTS index.
|
||||
Uses REPLACE to handle both new notes and updates.
|
||||
Indexes the note's title (articles, v2.16.0) or, for untitled notes, its
|
||||
first line. Uses REPLACE to handle both new notes and updates.
|
||||
|
||||
Args:
|
||||
conn: SQLite database connection
|
||||
note_id: Note ID (used as FTS rowid)
|
||||
slug: Note slug
|
||||
content: Full markdown content
|
||||
content: Markdown content
|
||||
title: The note's title, if it has one
|
||||
|
||||
Raises:
|
||||
sqlite3.Error: If FTS update fails
|
||||
"""
|
||||
# Extract title from first line
|
||||
lines = content.split('\n', 1)
|
||||
title = lines[0].strip() if lines else ''
|
||||
|
||||
# Remove markdown heading syntax (# ## ###)
|
||||
if title.startswith('#'):
|
||||
title = title.lstrip('#').strip()
|
||||
title = title or _first_line(content)
|
||||
|
||||
# Limit title length
|
||||
if len(title) > 100:
|
||||
@@ -170,7 +173,7 @@ def rebuild_fts_index(db_path: Path, data_dir: Path):
|
||||
|
||||
# Get all non-deleted notes
|
||||
cursor = conn.execute(
|
||||
"SELECT id, slug, file_path FROM notes WHERE deleted_at IS NULL"
|
||||
"SELECT id, slug, file_path, title FROM notes WHERE deleted_at IS NULL"
|
||||
)
|
||||
|
||||
indexed_count = 0
|
||||
@@ -188,7 +191,7 @@ def rebuild_fts_index(db_path: Path, data_dir: Path):
|
||||
content = note_path.read_text(encoding='utf-8')
|
||||
|
||||
# Update FTS index
|
||||
update_fts_index(conn, row['id'], row['slug'], content)
|
||||
update_fts_index(conn, row['id'], row['slug'], content, row['title'])
|
||||
indexed_count += 1
|
||||
|
||||
except Exception as e:
|
||||
@@ -408,6 +411,7 @@ def search_notes_fallback(
|
||||
id,
|
||||
slug,
|
||||
file_path,
|
||||
title,
|
||||
published,
|
||||
created_at
|
||||
FROM notes
|
||||
@@ -446,8 +450,8 @@ def search_notes_fallback(
|
||||
file_path = data_dir / row['file_path']
|
||||
content = read_note_file(file_path)
|
||||
|
||||
# Check if query matches content (case-insensitive)
|
||||
content_lower = content.lower()
|
||||
# Check if query matches title or content (case-insensitive)
|
||||
content_lower = f"{row['title'] or ''}\n{content}".lower()
|
||||
query_lower = query.lower()
|
||||
matches = query_lower in content_lower
|
||||
|
||||
@@ -456,11 +460,7 @@ def search_notes_fallback(
|
||||
matches = any(term.lower() in content_lower for term in terms)
|
||||
|
||||
if matches:
|
||||
# Extract title from first line
|
||||
lines = content.split('\n', 1)
|
||||
title = lines[0].strip() if lines else row['slug']
|
||||
if title.startswith('#'):
|
||||
title = title.lstrip('#').strip()
|
||||
title = row['title'] or _first_line(content) or row['slug']
|
||||
|
||||
results.append({
|
||||
'id': row['id'],
|
||||
|
||||
+26
-7
@@ -357,7 +357,9 @@ class BlueskyClient:
|
||||
def _record(self, post: "Post", text: Optional[str] = None, embed: Optional[dict] = None,
|
||||
anchors: List[tuple] = ()) -> dict:
|
||||
if text is None:
|
||||
body, anchors = plain_text_links(post.note.content, links="anchor")
|
||||
# An article is posted as a teaser (compose_text), so its links aren't in the text
|
||||
body, anchors = (None, []) if post.note.title else \
|
||||
plain_text_links(post.note.content, links="anchor")
|
||||
text = compose_text(post.note, post.permalink, post.tags, post.max_chars, body=body)
|
||||
record = {"$type": self.POST, "text": text, "createdAt": self._now()}
|
||||
facets = bluesky_facets(text, anchors)
|
||||
@@ -368,14 +370,16 @@ class BlueskyClient:
|
||||
elif post.media:
|
||||
record["embed"] = {"$type": "app.bsky.embed.images",
|
||||
"images": [self._image(m) for m in post.media]}
|
||||
elif note_links(post.note.content):
|
||||
elif not post.note.title and note_links(post.note.content):
|
||||
# The note links somewhere: the card shows that page, not the note
|
||||
record["embed"] = self._link_card(note_links(post.note.content)[0])
|
||||
else:
|
||||
# Articles always get a card for the article itself (v2.16.0)
|
||||
description = article_teaser(post.note) if post.note.title else plain_text(post.note.content)
|
||||
record["embed"] = {"$type": "app.bsky.embed.external", "external": {
|
||||
"uri": post.permalink,
|
||||
"title": post.note.title[:300],
|
||||
"description": plain_text(post.note.content)[:300],
|
||||
"title": post.note.label[:300],
|
||||
"description": description[:300],
|
||||
}}
|
||||
return record
|
||||
|
||||
@@ -1185,19 +1189,30 @@ def _hashtag(display_name: str) -> str:
|
||||
return f"#{word}" if word else ""
|
||||
|
||||
|
||||
def article_teaser(note) -> str:
|
||||
"""An article's summary, or the start of its text (v2.16.0)"""
|
||||
return note.summary or note.excerpt
|
||||
|
||||
|
||||
def compose_text(note, permalink: str, tags: List[str], max_chars: int,
|
||||
body: Optional[str] = None) -> str:
|
||||
"""
|
||||
Full note (Markdown unless `body` is given), then the permalink and
|
||||
hashtags; shortened at a word boundary if too long. On short targets the
|
||||
hashtags may use at most a third of the space.
|
||||
|
||||
An article (a note with a title) is posted as its title and teaser
|
||||
instead of its full text, the usual POSSE form for articles (v2.16.0).
|
||||
"""
|
||||
hashtags = ""
|
||||
for h in (_hashtag(t) for t in tags):
|
||||
if h and len(hashtags) + len(h) + 1 <= max(max_chars // 3, 1):
|
||||
hashtags = f"{hashtags} {h}".strip()
|
||||
tail = f"\n\n{permalink}" + (f"\n\n{hashtags}" if hashtags else "")
|
||||
body = (note.content if body is None else body).strip()
|
||||
if getattr(note, "title", None):
|
||||
body = f"{note.title}\n\n{article_teaser(note)}".strip()
|
||||
else:
|
||||
body = (note.content if body is None else body).strip()
|
||||
if getattr(note, "bookmark_of", None):
|
||||
# A bookmark leads with what it bookmarks (v2.9.0)
|
||||
body = f"🔖 {note.bookmark_of}\n\n{body}".strip()
|
||||
@@ -1215,7 +1230,10 @@ def _note_soup(markdown_text: str):
|
||||
import markdown
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
return BeautifulSoup(markdown.markdown(markdown_text, extensions=["fenced_code"]), "html.parser")
|
||||
from starpunk.markdown_ext import HashtagLineExtension
|
||||
|
||||
html = markdown.markdown(markdown_text, extensions=["fenced_code", HashtagLineExtension()])
|
||||
return BeautifulSoup(html, "html.parser")
|
||||
|
||||
|
||||
def _clean_url(url: str) -> str:
|
||||
@@ -1509,8 +1527,9 @@ def _reconcile(row, target: Target) -> tuple:
|
||||
permalink=f"{site}/note/{note.slug}",
|
||||
tags=[t["display_name"] for t in get_note_tags(note.id, entered_order=True)],
|
||||
max_chars=target.max_chars,
|
||||
# Articles are posted as a teaser without images (v2.16.0)
|
||||
media=_media_files(note.id, target.max_media, note.content)
|
||||
if note.kind == "note" else [],
|
||||
if note.kind == "note" and not note.title else [],
|
||||
kind=note.kind,
|
||||
target_url=note.reacts_to,
|
||||
target_title=_target_title(note.reacts_to),
|
||||
|
||||
@@ -480,6 +480,27 @@ def validate_note_path(file_path: Path, data_dir: Path) -> bool:
|
||||
# Atomic File Operations
|
||||
|
||||
|
||||
def find_trashed_file(data_dir: Path, file_path: str) -> Optional[Path]:
|
||||
"""
|
||||
Where delete_note_file put a note's file: .trash/<year>/<month>/<name>
|
||||
|
||||
Args:
|
||||
data_dir: Data directory
|
||||
file_path: The note's file_path as stored in the database
|
||||
|
||||
Returns:
|
||||
Path of the trashed file, or None if it can't be found
|
||||
"""
|
||||
parts = Path(file_path).parts
|
||||
name = Path(file_path).name
|
||||
candidates = []
|
||||
if "notes" in parts and len(parts) > parts.index("notes") + 2:
|
||||
i = parts.index("notes")
|
||||
candidates.append(data_dir / TRASH_DIR_NAME / parts[i + 1] / parts[i + 2] / name)
|
||||
candidates += sorted((data_dir / TRASH_DIR_NAME).glob(f"*/*/{name}"))
|
||||
return next((c for c in candidates if c.exists()), None)
|
||||
|
||||
|
||||
def write_note_file(file_path: Path, content: str) -> None:
|
||||
"""
|
||||
Write note content to file atomically
|
||||
|
||||
@@ -51,7 +51,7 @@
|
||||
<tr>
|
||||
<td class="note-content-preview">
|
||||
<div class="note-excerpt">
|
||||
{{ note.content[:100] }}{% if note.content|length > 100 %}...{% endif %}
|
||||
{% if note.title %}<strong>{{ note.title }}</strong>{% else %}{{ note.content[:100] }}{% if note.content|length > 100 %}...{% endif %}{% endif %}
|
||||
</div>
|
||||
<small class="note-slug">{{ note.slug }}</small>
|
||||
</td>
|
||||
|
||||
@@ -11,6 +11,12 @@
|
||||
</p>
|
||||
|
||||
<form action="{{ url_for('admin.update_note_submit', note_id=note.id) }}" method="POST" class="note-form"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
||||
<div class="form-group">
|
||||
<label for="title">Title (optional)</label>
|
||||
<input type="text" id="title" name="title" value="{{ note.title or '' }}" maxlength="200"
|
||||
placeholder="Leave blank for a note; a title makes it an article">
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
{% if note.reacts_to %}
|
||||
<p class="note-meta">{{ {'like': '★ Like of', 'repost': '🔁 Repost of', 'bookmark': '🔖 Bookmark of'}[note.kind] }} <a href="{{ note.reacts_to }}">{{ note.reacts_to }}</a></p>
|
||||
@@ -20,12 +26,18 @@
|
||||
id="content"
|
||||
name="content"
|
||||
rows="{{ 6 if note.reacts_to else 20 }}"
|
||||
{% if not note.reacts_to %}required{% endif %}
|
||||
autofocus
|
||||
>{{ note.content }}</textarea>
|
||||
<small>Use Markdown syntax for formatting</small>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="summary">Summary (optional)</label>
|
||||
<textarea id="summary" name="summary" rows="2"
|
||||
placeholder="A sentence or two for the homepage, feeds and crossposts">{{ note.summary or '' }}</textarea>
|
||||
<small>Articles only. Leave blank to use the start of the text.</small>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="slug">Slug (permanent)</label>
|
||||
<input type="text"
|
||||
|
||||
@@ -7,6 +7,12 @@
|
||||
<h2>Create New Note</h2>
|
||||
|
||||
<form action="{{ url_for('admin.create_note_submit') }}" method="POST" enctype="multipart/form-data" class="note-form"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
||||
<div class="form-group">
|
||||
<label for="title">Title (optional)</label>
|
||||
<input type="text" id="title" name="title" value="" maxlength="200"
|
||||
placeholder="Leave blank for a note; a title makes it an article">
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="content">Content (Markdown)</label>
|
||||
<textarea
|
||||
@@ -14,12 +20,18 @@
|
||||
name="content"
|
||||
rows="20"
|
||||
placeholder="Write your note in markdown..."
|
||||
required
|
||||
autofocus
|
||||
></textarea>
|
||||
<small>Use Markdown syntax for formatting</small>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="summary">Summary (optional)</label>
|
||||
<textarea id="summary" name="summary" rows="2"
|
||||
placeholder="A sentence or two for the homepage, feeds and crossposts"></textarea>
|
||||
<small>Articles only. Leave blank to use the start of the text.</small>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="custom_slug">Custom Slug (optional)</label>
|
||||
<input type="text"
|
||||
|
||||
+7
-7
@@ -3,17 +3,17 @@
|
||||
{% from "partials/reaction.html" import reaction %}
|
||||
{% import "partials/responses.html" as responses with context %}
|
||||
|
||||
{% block title %}{% if note.has_title %}{{ note.title }}{% elif note.excerpt.strip() %}{{ note.excerpt|truncate(60) }}{% elif note.repost_of %}Repost{% elif note.like_of %}Like{% elif note.bookmark_of %}Bookmark{% elif note.in_reply_to %}Reply{% else %}Photo{% endif %} · {{ config.SITE_NAME }}{% endblock %}
|
||||
{% block title %}{% if note.title %}{{ note.title }}{% elif note.excerpt.strip() %}{{ note.excerpt|truncate(60) }}{% elif note.repost_of %}Repost{% elif note.like_of %}Like{% elif note.bookmark_of %}Bookmark{% elif note.in_reply_to %}Reply{% else %}Photo{% endif %} · {{ config.SITE_NAME }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<article class="h-entry">
|
||||
{# Notes starting with a '# Heading' have a title (Q22; Note.has_title) #}
|
||||
{% set has_explicit_title = note.has_title %}
|
||||
|
||||
{# p-name only if note has explicit title (per Q22) #}
|
||||
{% if has_explicit_title %}
|
||||
{# Titled notes are articles: p-name only then, so notes get no implied name (ADR-076) #}
|
||||
{% if note.title %}
|
||||
<h1 class="p-name">{{ note.title }}</h1>
|
||||
{% endif %}
|
||||
{% if note.summary %}
|
||||
<p class="p-summary">{{ note.summary }}</p>
|
||||
{% endif %}
|
||||
|
||||
{# Reply context (v1.6.0 Phase 4) - displayed if this is a reply post #}
|
||||
{% if note.in_reply_to %}
|
||||
@@ -49,7 +49,7 @@
|
||||
|
||||
{# e-content: note content BELOW media (per ADR-057) #}
|
||||
<div class="e-content">
|
||||
{{ note.body_html|safe }}
|
||||
{{ note.html|safe }}
|
||||
</div>
|
||||
|
||||
<footer class="note-meta">
|
||||
|
||||
@@ -4,9 +4,8 @@
|
||||
{% from "partials/responses.html" import list_counts %}
|
||||
{% macro note_preview(note, author) %}
|
||||
<article class="h-entry note-preview">
|
||||
{# Notes starting with a '# Heading' have a title (Q22; Note.has_title) #}
|
||||
{% set has_explicit_title = note.has_title %}
|
||||
{% if has_explicit_title %}
|
||||
{# Titled notes are articles (ADR-076) #}
|
||||
{% if note.title %}
|
||||
<h2 class="p-name"><a href="{{ absolute_url_for('public.note', slug=note.slug) }}">{{ note.title }}</a></h2>
|
||||
{% endif %}
|
||||
|
||||
@@ -16,15 +15,19 @@
|
||||
|
||||
{{ reaction(note) }}
|
||||
|
||||
{{ display_media(note.media, note.excerpt|truncate(150)) }}
|
||||
|
||||
{% if has_explicit_title %}
|
||||
{# Titled notes are articles: show a summary and link through #}
|
||||
{% if note.title %}
|
||||
{# Articles: title, summary and a link through; images stay on the article page (v2.16.0).
|
||||
The photos stay in the markup, unseen, so parsers read the same post type here as on the page. #}
|
||||
{% for item in note.media or [] %}<data class="u-photo" value="{{ url_for('public.media_file', path=item.path) }}"></data>{% endfor %}
|
||||
{% set summary = note.summary or note.excerpt %}
|
||||
{% if summary.strip() %}
|
||||
<div class="p-summary">
|
||||
<p>{{ note.excerpt }}</p>
|
||||
<p>{{ summary }}</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
<p class="read-more"><a href="{{ absolute_url_for('public.note', slug=note.slug) }}">Read more →</a></p>
|
||||
{% else %}
|
||||
{{ display_media(note.media, note.excerpt|truncate(150)) }}
|
||||
{# Always present, even empty: without it a text-free photo post gets an implied name (mf2) #}
|
||||
<div class="e-content">
|
||||
{{ note.html|safe }}
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
"""
|
||||
Articles: titles and summaries stored apart from the text (v2.16.0, ADR-076).
|
||||
|
||||
Feeds give only articles a title and never repeat it in the content; the
|
||||
homepage and tag pages show an article as title, summary and "Read more"
|
||||
without its images; the admin editor has Title and Summary fields.
|
||||
"""
|
||||
|
||||
import json
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
import mf2py
|
||||
|
||||
from starpunk.auth import create_session
|
||||
from starpunk.notes import create_note, get_note
|
||||
from tests.test_micropub_v240 import SITE, app, client, h # noqa: F401
|
||||
from tests.test_micropub_v265 import upload
|
||||
|
||||
ATOM = {"a": "http://www.w3.org/2005/Atom"}
|
||||
|
||||
|
||||
def micropub(client, h, **props):
|
||||
response = client.post("/micropub", json={"type": ["h-entry"], "properties": props}, headers=h)
|
||||
assert response.status_code == 201, response.get_json()
|
||||
return response.headers["Location"]
|
||||
|
||||
|
||||
def make_both(app):
|
||||
with app.app_context():
|
||||
article = create_note(content="The **body**.", title="An Article", summary="The gist.",
|
||||
published=True, tags=["essay"])
|
||||
note = create_note(content="# Just a heading\n\nin a note", published=True, tags=["essay"])
|
||||
return article, note
|
||||
|
||||
|
||||
class TestFeeds:
|
||||
def test_rss_title_only_for_articles(self, app, client):
|
||||
article, note = make_both(app)
|
||||
items = {i.find("link").text: i for i in ET.fromstring(client.get("/feed.rss").data).iter("item")}
|
||||
a, n = items[f"{SITE}/note/{article.slug}"], items[f"{SITE}/note/{note.slug}"]
|
||||
assert a.find("title").text == "An Article"
|
||||
assert "An Article" not in a.find("description").text # not repeated in the body
|
||||
assert n.find("title") is None
|
||||
assert n.find("description") is not None
|
||||
|
||||
def test_atom_empty_title_and_summary(self, app, client):
|
||||
article, note = make_both(app)
|
||||
entries = {e.find("a:id", ATOM).text: e
|
||||
for e in ET.fromstring(client.get("/feed.atom").data).findall("a:entry", ATOM)}
|
||||
a, n = entries[f"{SITE}/note/{article.slug}"], entries[f"{SITE}/note/{note.slug}"]
|
||||
assert a.find("a:title", ATOM).text == "An Article"
|
||||
assert a.find("a:summary", ATOM).text == "The gist."
|
||||
assert "An Article" not in a.find("a:content", ATOM).text
|
||||
assert n.find("a:title", ATOM) is not None and not n.find("a:title", ATOM).text
|
||||
assert n.find("a:summary", ATOM) is None
|
||||
|
||||
def test_json_feed(self, app, client):
|
||||
article, note = make_both(app)
|
||||
items = {i["url"]: i for i in json.loads(client.get("/feed.json").data)["items"]}
|
||||
a, n = items[f"{SITE}/note/{article.slug}"], items[f"{SITE}/note/{note.slug}"]
|
||||
assert (a["title"], a["summary"]) == ("An Article", "The gist.")
|
||||
assert "An Article" not in a["content_html"]
|
||||
assert "title" not in n and "summary" not in n
|
||||
assert "<h1>Just a heading</h1>" in n["content_html"]
|
||||
|
||||
|
||||
class TestPages:
|
||||
def test_article_page(self, app, client):
|
||||
article, _ = make_both(app)
|
||||
(entry,) = [i for i in mf2py.parse(doc=client.get(f"/note/{article.slug}").data.decode(),
|
||||
url=f"{SITE}/note/{article.slug}")["items"]
|
||||
if "h-entry" in i["type"]]
|
||||
props = entry["properties"]
|
||||
assert props["name"] == ["An Article"]
|
||||
assert props["summary"] == ["The gist."]
|
||||
assert not props["content"][0]["value"].startswith("An Article")
|
||||
|
||||
def test_homepage_and_tag_page(self, app, client, h):
|
||||
photo = upload(client, h)
|
||||
url = micropub(client, h, name=["Trip"], content=["Day one of the trip."], photo=[photo],
|
||||
category=["essay"])
|
||||
micropub(client, h, content=["A photo note"], photo=[upload(client, h, "blue")], category=["essay"])
|
||||
with app.app_context():
|
||||
slug = url.rsplit("/", 1)[1]
|
||||
article = get_note(slug=slug)
|
||||
for path in ("/", "/tag/essay"):
|
||||
html = client.get(path).data.decode()
|
||||
start = html.index(f'href="{url}"')
|
||||
section = html[html.rindex("<article", 0, start):html.index("</article>", start)]
|
||||
assert "Trip" in section and "Day one of the trip." in section and "Read more" in section
|
||||
assert "<img" not in section and "e-content" not in section
|
||||
assert 'class="u-photo"' in section # parsers still see a photo post
|
||||
assert html.count("<img") >= 1 # the photo note still shows its image
|
||||
assert article.title == "Trip"
|
||||
|
||||
def test_preview_uses_summary_over_excerpt(self, app, client):
|
||||
make_both(app)
|
||||
html = client.get("/").data.decode()
|
||||
assert "The gist." in html and "The <strong>body</strong>." not in html
|
||||
|
||||
|
||||
class TestAdmin:
|
||||
def login(self, app, client):
|
||||
with app.test_request_context():
|
||||
client.set_cookie("starpunk_session", create_session(app.config["ADMIN_ME"]))
|
||||
|
||||
def test_create_and_edit_article(self, app, client):
|
||||
self.login(app, client)
|
||||
response = client.post("/admin/new", data={
|
||||
"title": "From the editor", "summary": "Short.", "content": "Body", "published": "on"})
|
||||
assert response.status_code == 302
|
||||
with app.app_context():
|
||||
from starpunk.notes import list_notes
|
||||
(note,) = list_notes()
|
||||
assert (note.title, note.summary, note.content) == ("From the editor", "Short.", "Body")
|
||||
|
||||
form = client.get(f"/admin/edit/{note.id}").data.decode()
|
||||
assert 'value="From the editor"' in form and ">Short.</textarea>" in form
|
||||
|
||||
client.post(f"/admin/edit/{note.id}", data={"title": "", "summary": "", "content": "Body",
|
||||
"published": "on"})
|
||||
with app.app_context():
|
||||
note = get_note(id=note.id)
|
||||
assert (note.title, note.summary) == (None, None)
|
||||
|
||||
def test_title_only_article(self, app, client):
|
||||
self.login(app, client)
|
||||
client.post("/admin/new", data={"title": "Only a title", "content": "", "published": "on"})
|
||||
with app.app_context():
|
||||
from starpunk.notes import list_notes
|
||||
(note,) = list_notes()
|
||||
assert note.title == "Only a title" and note.content == ""
|
||||
+15
-29
@@ -55,7 +55,8 @@ def sample_notes(app):
|
||||
notes = []
|
||||
for i in range(5):
|
||||
note = create_note(
|
||||
content=f"# Test Note {i}\n\nThis is test content for note {i}.",
|
||||
content=f"This is test content for note {i}.",
|
||||
title=f"Test Note {i}",
|
||||
published=True,
|
||||
)
|
||||
notes.append(note)
|
||||
@@ -141,7 +142,8 @@ class TestGenerateFeed:
|
||||
import time
|
||||
for i in range(3):
|
||||
create_note(
|
||||
content=f"# Note {i}\n\nContent {i}.",
|
||||
content=f"Content {i}.",
|
||||
title=f"Note {i}",
|
||||
published=True,
|
||||
)
|
||||
time.sleep(0.01) # Ensure distinct timestamps
|
||||
@@ -342,41 +344,25 @@ class TestFormatRFC822Date:
|
||||
class TestGetNoteTitle:
|
||||
"""Test get_note_title() function"""
|
||||
|
||||
def test_get_note_title_with_heading(self, app):
|
||||
"""Test title extraction from note with heading"""
|
||||
def test_get_note_title_article(self, app):
|
||||
"""An article's item title is its title"""
|
||||
with app.app_context():
|
||||
note = create_note(content="# My First Note\n\nContent here.", published=True)
|
||||
title = get_note_title(note)
|
||||
assert title == "My First Note"
|
||||
note = create_note(content="Content here.", title="My First Note", published=True)
|
||||
assert get_note_title(note) == "My First Note"
|
||||
|
||||
def test_get_note_title_without_heading(self, app):
|
||||
"""Test title extraction from note without heading"""
|
||||
def test_get_note_title_untitled_note(self, app):
|
||||
"""Untitled notes have no item title (v2.16.0, ADR-076)"""
|
||||
with app.app_context():
|
||||
note = create_note(content="Just some content without heading.", published=True)
|
||||
title = get_note_title(note)
|
||||
|
||||
# Should use first line (Note.title handles this)
|
||||
assert len(title) > 0
|
||||
assert "Just some content" in title
|
||||
assert get_note_title(note) is None
|
||||
note = create_note(content="# A heading in the body\n\nis not a title", published=True)
|
||||
assert get_note_title(note) is None
|
||||
|
||||
def test_get_note_title_truncates_long_titles(self, app):
|
||||
"""Test title truncation for long titles"""
|
||||
with app.app_context():
|
||||
long_title = "A" * 150
|
||||
note = create_note(content=f"# {long_title}\n\nContent.", published=True)
|
||||
title = get_note_title(note)
|
||||
|
||||
# Should truncate to reasonable length
|
||||
assert len(title) <= 103 # 100 chars + "..."
|
||||
|
||||
def test_get_note_title_minimal_content(self, app):
|
||||
"""Test title extraction with minimal content"""
|
||||
with app.app_context():
|
||||
note = create_note(content="x", published=True)
|
||||
title = get_note_title(note)
|
||||
|
||||
# Should extract something (single character or slug)
|
||||
assert len(title) > 0
|
||||
note = create_note(content="Content.", title="A" * 150, published=True)
|
||||
assert len(get_note_title(note)) <= 103 # 100 chars + "..."
|
||||
|
||||
|
||||
class TestCleanHTMLForRSS:
|
||||
|
||||
@@ -49,7 +49,7 @@ def sample_notes(app):
|
||||
notes = []
|
||||
for i in range(5):
|
||||
note = create_note(
|
||||
content=f"# Test Note {i}\n\nThis is test content for note {i}.",
|
||||
title=f"Test Note {i}", content=f"This is test content for note {i}.",
|
||||
published=True,
|
||||
)
|
||||
notes.append(note)
|
||||
@@ -138,7 +138,7 @@ class TestGenerateAtom:
|
||||
# Create notes with distinct timestamps
|
||||
for i in range(3):
|
||||
create_note(
|
||||
content=f"# Note {i}\n\nContent {i}.",
|
||||
title=f"Note {i}", content=f"Content {i}.",
|
||||
published=True,
|
||||
)
|
||||
time.sleep(0.01)
|
||||
@@ -216,7 +216,7 @@ class TestGenerateAtom:
|
||||
"""Test ATOM feed includes HTML content properly escaped"""
|
||||
with app.app_context():
|
||||
note = create_note(
|
||||
content="# Test\n\nThis is **bold** and *italic*.",
|
||||
title="Test", content="This is **bold** and *italic*.",
|
||||
published=True,
|
||||
)
|
||||
|
||||
@@ -243,7 +243,7 @@ class TestGenerateAtom:
|
||||
"""Test ATOM feed escapes special XML characters"""
|
||||
with app.app_context():
|
||||
note = create_note(
|
||||
content="# Test & Special <Characters>\n\nContent with 'quotes' and \"doubles\".",
|
||||
title="Test & Special <Characters>", content="Content with 'quotes' and \"doubles\".",
|
||||
published=True,
|
||||
)
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ def sample_notes(app):
|
||||
notes = []
|
||||
for i in range(5):
|
||||
note = create_note(
|
||||
content=f"# Test Note {i}\n\nThis is test content for note {i}.",
|
||||
title=f"Test Note {i}", content=f"This is test content for note {i}.",
|
||||
published=True,
|
||||
)
|
||||
notes.append(note)
|
||||
@@ -121,7 +121,7 @@ class TestGenerateJsonFeed:
|
||||
# Create notes with distinct timestamps
|
||||
for i in range(3):
|
||||
create_note(
|
||||
content=f"# Note {i}\n\nContent {i}.",
|
||||
title=f"Note {i}", content=f"Content {i}.",
|
||||
published=True,
|
||||
)
|
||||
time.sleep(0.01)
|
||||
@@ -195,7 +195,7 @@ class TestGenerateJsonFeed:
|
||||
"""Test JSON Feed includes HTML content"""
|
||||
with app.app_context():
|
||||
note = create_note(
|
||||
content="# Test\n\nThis is **bold** and *italic*.",
|
||||
title="Test", content="This is **bold** and *italic*.",
|
||||
published=True,
|
||||
)
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ def note_with_single_media(app):
|
||||
with app.app_context():
|
||||
# Create note
|
||||
note = create_note(
|
||||
content="# Test Note\n\nNote with one image.",
|
||||
title="Test Note", content="Note with one image.",
|
||||
published=True
|
||||
)
|
||||
|
||||
@@ -106,7 +106,7 @@ def note_with_multiple_media(app):
|
||||
with app.app_context():
|
||||
# Create note
|
||||
note = create_note(
|
||||
content="# Gallery Note\n\nNote with three images.",
|
||||
title="Gallery Note", content="Note with three images.",
|
||||
published=True
|
||||
)
|
||||
|
||||
@@ -135,7 +135,7 @@ def note_without_media(app):
|
||||
"""Create note without media"""
|
||||
with app.app_context():
|
||||
note = create_note(
|
||||
content="# Plain Note\n\nNote without images.",
|
||||
title="Plain Note", content="Note without images.",
|
||||
published=True
|
||||
)
|
||||
notes = list_notes(published_only=True, limit=1)
|
||||
|
||||
@@ -57,13 +57,14 @@ class TestNoteHEntry:
|
||||
assert props['url'][0] == props['uid'][0], "u-url and u-uid should match"
|
||||
|
||||
def test_hentry_pname_only_with_explicit_title(self, client, app, data_dir):
|
||||
"""p-name only present when note has explicit title (per Q22)"""
|
||||
"""p-name only present when note has a title (ADR-076, was Q22)"""
|
||||
from starpunk.notes import create_note
|
||||
|
||||
# Create note WITH heading (explicit title)
|
||||
# Create note WITH a title (an article)
|
||||
with app.app_context():
|
||||
note_with_title = create_note(
|
||||
content="# Explicit Title\n\nThis note has a heading.",
|
||||
content="This note has a title.",
|
||||
title="Explicit Title",
|
||||
custom_slug="note-with-title",
|
||||
published=True
|
||||
)
|
||||
|
||||
@@ -142,7 +142,8 @@ def test_split_title(content, title):
|
||||
@pytest.mark.parametrize("content,kind", [
|
||||
("#indieweb is great, posting from my phone", "note"),
|
||||
("#\n\nbody text", "note"),
|
||||
("# A Title\n\nThe body", "article"),
|
||||
# A heading in the text is no longer a title (v2.16.0, ADR-076)
|
||||
("# A Title\n\nThe body", "note"),
|
||||
])
|
||||
def test_post_type_of_hash_notes(app, client, content, kind):
|
||||
note = make_note(app, content)
|
||||
|
||||
@@ -167,11 +167,11 @@ def test_micropub_create_with_name(client, app, mock_valid_token):
|
||||
assert response.status_code == 201
|
||||
assert 'Location' in response.headers
|
||||
|
||||
# StarPunk titles are a '# Heading' first line, so name must land there
|
||||
# name is stored as the title, apart from the content (v2.16.0, ADR-076)
|
||||
slug = response.headers['Location'].rstrip('/').split('/')[-1]
|
||||
with app.app_context():
|
||||
note = get_note(slug)
|
||||
assert note.content == '# My Test Title\n\nContent goes here'
|
||||
assert note.content == 'Content goes here'
|
||||
assert note.title == 'My Test Title'
|
||||
|
||||
|
||||
@@ -193,7 +193,8 @@ def test_micropub_create_with_name_already_heading(client, app, mock_valid_token
|
||||
assert response.status_code == 201
|
||||
slug = response.headers['Location'].rstrip('/').split('/')[-1]
|
||||
with app.app_context():
|
||||
assert get_note(slug).content == '# Same Title\n\nBody'
|
||||
note = get_note(slug)
|
||||
assert (note.title, note.content) == ('Same Title', 'Body')
|
||||
|
||||
|
||||
def test_micropub_create_with_categories(client, app, mock_valid_token):
|
||||
|
||||
@@ -114,9 +114,29 @@ class TestUpdate:
|
||||
def test_replace_and_delete_name(self, app, client, h):
|
||||
url = create(client, h)
|
||||
self.update(client, h, url, replace={"name": ["Added"]})
|
||||
assert note_at(app, url).content.startswith("# Added")
|
||||
note = note_at(app, url)
|
||||
assert note.title == "Added" and not note.content.startswith("#")
|
||||
self.update(client, h, url, delete=["name"])
|
||||
assert "name" not in source(client, h, url)["properties"]
|
||||
assert note_at(app, url).title is None
|
||||
|
||||
def test_summary(self, app, client, h):
|
||||
"""summary is stored alongside content, updatable and returned by q=source (v2.16.0)"""
|
||||
url = create(client, h, name=["Title"], summary=["Short version"])
|
||||
props = source(client, h, url)["properties"]
|
||||
assert props["summary"] == ["Short version"] and props["name"] == ["Title"]
|
||||
assert self.update(client, h, url, replace={"summary": ["Shorter"]}).status_code == 204
|
||||
assert note_at(app, url).summary == "Shorter"
|
||||
assert self.update(client, h, url, delete=["summary"]).status_code == 204
|
||||
assert "summary" not in source(client, h, url)["properties"]
|
||||
|
||||
def test_article_body_may_go(self, app, client, h):
|
||||
"""A titled post may have no text, like a photo post (v2.16.0)"""
|
||||
url = create(client, h, name=["Only a title"])
|
||||
assert self.update(client, h, url, delete=["content"]).status_code == 204
|
||||
assert note_at(app, url).content == ""
|
||||
url = create(client, h)
|
||||
assert self.update(client, h, url, delete=["content"]).status_code == 400
|
||||
|
||||
def test_categories(self, app, client, h):
|
||||
url = create(client, h, category=["one", "two"])
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
"""
|
||||
Migration 023 moves '# Heading' titles out of note files into notes.title (v2.16.0, ADR-076).
|
||||
|
||||
Starts from a fully migrated database, takes it back to before 023, then
|
||||
runs the migrations again with the data directory.
|
||||
"""
|
||||
|
||||
import sqlite3
|
||||
|
||||
import pytest
|
||||
|
||||
from starpunk import create_app
|
||||
from starpunk.migrations import MigrationError, run_migrations
|
||||
from starpunk.utils import calculate_content_hash
|
||||
|
||||
MIGRATION = "023_note_titles.sql"
|
||||
TITLED = "# Old Title\n\nThe body."
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def setup(tmp_path):
|
||||
data = tmp_path / "data"
|
||||
data.mkdir()
|
||||
db_path = data / "starpunk.db"
|
||||
create_app(config={"TESTING": True, "DATABASE_PATH": db_path, "DATA_PATH": data,
|
||||
"SESSION_SECRET": "x", "ADMIN_ME": "https://example.com",
|
||||
"SITE_URL": "https://blog.example.com"})
|
||||
|
||||
files = {
|
||||
"titled": ("notes/2026/11/titled.md", TITLED, None),
|
||||
"untitled": ("notes/2026/11/untitled.md", "#indieweb is great", None),
|
||||
"trashed": ("notes/2026/10/trashed.md", "# Gone\n\nBin.", "2026-11-01"),
|
||||
}
|
||||
conn = sqlite3.connect(db_path)
|
||||
for slug, (rel, text, deleted_at) in files.items():
|
||||
path = data / rel
|
||||
if deleted_at: # delete_note_file put it in .trash/<year>/<month>/
|
||||
path = data / ".trash" / "2026" / "10" / "trashed.md"
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(text)
|
||||
conn.execute(
|
||||
"INSERT INTO notes (slug, file_path, published, created_at, updated_at, deleted_at,"
|
||||
" content_hash) VALUES (?, ?, 1, '2026-11-01', '2026-11-01', ?, ?)",
|
||||
(slug, rel, deleted_at, calculate_content_hash(text)),
|
||||
)
|
||||
conn.execute("INSERT INTO notes_fts (rowid, slug, title, content) VALUES (1, 'titled', 'x', 'y')")
|
||||
# Back to before migration 023
|
||||
conn.execute("ALTER TABLE notes DROP COLUMN title")
|
||||
conn.execute("ALTER TABLE notes DROP COLUMN summary")
|
||||
conn.execute("DELETE FROM schema_migrations WHERE migration_name = ?", (MIGRATION,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return db_path, data
|
||||
|
||||
|
||||
def notes(db_path):
|
||||
conn = sqlite3.connect(db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
rows = {r["slug"]: dict(r) for r in conn.execute("SELECT * FROM notes")}
|
||||
conn.close()
|
||||
return rows
|
||||
|
||||
|
||||
def test_titles_move_out_of_files(setup):
|
||||
db_path, data = setup
|
||||
run_migrations(db_path, data_dir=data)
|
||||
|
||||
rows = notes(db_path)
|
||||
assert rows["titled"]["title"] == "Old Title"
|
||||
assert (data / "notes/2026/11/titled.md").read_text() == "The body."
|
||||
assert rows["titled"]["content_hash"] == calculate_content_hash("The body.")
|
||||
|
||||
assert rows["untitled"]["title"] is None # a hashtag isn't a heading
|
||||
assert (data / "notes/2026/11/untitled.md").read_text() == "#indieweb is great"
|
||||
|
||||
assert rows["trashed"]["title"] == "Gone" # found in .trash, so undelete still works
|
||||
assert (data / ".trash/2026/10/trashed.md").read_text() == "Bin."
|
||||
|
||||
conn = sqlite3.connect(db_path)
|
||||
assert conn.execute("SELECT COUNT(*) FROM notes_fts").fetchone()[0] == 0 # rebuilt at startup
|
||||
assert conn.execute("SELECT 1 FROM schema_migrations WHERE migration_name = ?",
|
||||
(MIGRATION,)).fetchone()
|
||||
conn.close()
|
||||
|
||||
|
||||
def test_runs_once(setup):
|
||||
"""Afterwards a heading in a note's text is just a heading"""
|
||||
db_path, data = setup
|
||||
run_migrations(db_path, data_dir=data)
|
||||
(data / "notes/2026/11/untitled.md").write_text("# Now a heading\n\nNew text")
|
||||
run_migrations(db_path, data_dir=data)
|
||||
assert notes(db_path)["untitled"]["title"] is None
|
||||
assert (data / "notes/2026/11/untitled.md").read_text() == "# Now a heading\n\nNew text"
|
||||
|
||||
|
||||
def test_failure_puts_files_back(setup, monkeypatch):
|
||||
db_path, data = setup
|
||||
import starpunk.utils as utils
|
||||
|
||||
real_write = utils.write_note_file
|
||||
written = []
|
||||
|
||||
def write_then_fail(path, content):
|
||||
if written:
|
||||
raise OSError("disk full")
|
||||
written.append(path)
|
||||
real_write(path, content)
|
||||
|
||||
monkeypatch.setattr(utils, "write_note_file", write_then_fail)
|
||||
with pytest.raises(MigrationError):
|
||||
run_migrations(db_path, data_dir=data)
|
||||
|
||||
assert written # one file was changed before the failure...
|
||||
assert (data / "notes/2026/11/titled.md").read_text() == TITLED # ...and put back
|
||||
assert (data / ".trash/2026/10/trashed.md").read_text() == "# Gone\n\nBin."
|
||||
conn = sqlite3.connect(db_path)
|
||||
assert not conn.execute("SELECT 1 FROM schema_migrations WHERE migration_name = ?",
|
||||
(MIGRATION,)).fetchone()
|
||||
assert "title" not in [r[1] for r in conn.execute("PRAGMA table_info(notes)")]
|
||||
conn.close()
|
||||
|
||||
|
||||
def test_without_data_dir_only_schema(setup):
|
||||
db_path, data = setup
|
||||
run_migrations(db_path)
|
||||
assert notes(db_path)["titled"]["title"] is None
|
||||
assert (data / "notes/2026/11/titled.md").read_text() == TITLED
|
||||
+34
-48
@@ -11,6 +11,7 @@ Organized by model:
|
||||
import pytest
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
from dataclasses import replace
|
||||
from starpunk.models import Note, Session, Token, AuthState, split_title
|
||||
from starpunk.utils import calculate_content_hash
|
||||
|
||||
@@ -139,13 +140,12 @@ class TestNoteModel:
|
||||
html2 = note.html
|
||||
assert html1 == html2
|
||||
|
||||
def test_title_extraction_from_heading(self, tmp_path):
|
||||
"""Test title extraction from markdown heading"""
|
||||
def test_title_is_the_column_not_the_content(self, tmp_path):
|
||||
"""A title comes from notes.title; a heading in the content is just a heading (ADR-076)"""
|
||||
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
||||
note_file.parent.mkdir(parents=True)
|
||||
note_file.write_text("# My Note Title\n\nContent.")
|
||||
|
||||
note = Note(
|
||||
note_file.write_text("# A Heading\n\nContent.")
|
||||
kwargs = dict(
|
||||
id=1,
|
||||
slug="test",
|
||||
file_path="notes/2024/11/test.md",
|
||||
@@ -154,29 +154,13 @@ class TestNoteModel:
|
||||
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
||||
_data_dir=tmp_path,
|
||||
)
|
||||
assert Note(**kwargs).title is None
|
||||
assert "<h1>A Heading</h1>" in Note(**kwargs).html
|
||||
assert Note(**kwargs, title="My Note Title").title == "My Note Title"
|
||||
|
||||
assert note.title == "My Note Title"
|
||||
|
||||
def test_title_extraction_from_first_line(self, tmp_path):
|
||||
"""Test title extraction from first line without heading"""
|
||||
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
||||
note_file.parent.mkdir(parents=True)
|
||||
note_file.write_text("Just a note without heading\n\nMore content.")
|
||||
|
||||
note = Note(
|
||||
id=1,
|
||||
slug="test",
|
||||
file_path="notes/2024/11/test.md",
|
||||
published=True,
|
||||
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
||||
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
||||
_data_dir=tmp_path,
|
||||
)
|
||||
|
||||
assert note.title == "Just a note without heading"
|
||||
|
||||
def test_title_fallback_to_slug(self):
|
||||
"""Test title falls back to slug if no content"""
|
||||
def test_label(self, tmp_path):
|
||||
"""label: title, else excerpt, else slug"""
|
||||
assert self._note_with(tmp_path, "Just a note\n\nMore.").label == "Just a note More."
|
||||
note = Note(
|
||||
id=1,
|
||||
slug="my-test-note",
|
||||
@@ -185,10 +169,10 @@ class TestNoteModel:
|
||||
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
||||
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
||||
_data_dir=Path("/nonexistent"),
|
||||
title="Titled",
|
||||
)
|
||||
|
||||
# Should fall back to slug (file doesn't exist)
|
||||
assert note.title == "my-test-note"
|
||||
assert note.label == "Titled"
|
||||
assert replace(note, title=None).label == "my-test-note"
|
||||
|
||||
def test_excerpt_generation(self, tmp_path):
|
||||
"""Test excerpt generation"""
|
||||
@@ -208,8 +192,8 @@ class TestNoteModel:
|
||||
)
|
||||
|
||||
excerpt = note.excerpt
|
||||
assert len(excerpt) <= 203 # EXCERPT_LENGTH + "..."
|
||||
assert excerpt.endswith("...")
|
||||
assert len(excerpt) <= 201 # EXCERPT_LENGTH + "…"
|
||||
assert excerpt.endswith("…")
|
||||
|
||||
def test_excerpt_short_content(self, tmp_path):
|
||||
"""Test excerpt with short content (no ellipsis)"""
|
||||
@@ -259,6 +243,24 @@ class TestNoteModel:
|
||||
_data_dir=tmp_path,
|
||||
)
|
||||
|
||||
def test_excerpt_is_plain_text(self, tmp_path):
|
||||
"""No Markdown in previews, alt text or crosspost teasers (v2.16.0)"""
|
||||
note = self._note_with(
|
||||
tmp_path, "Some **bold** and a [link](https://x.example).\n\n\n\n- one\n- two")
|
||||
assert note.excerpt == "Some bold and a link. • one • two"
|
||||
|
||||
def test_hashtag_line_is_not_a_heading(self, tmp_path):
|
||||
"""'#indieweb ...' renders as text; '# Heading' still a heading (v2.16.0)"""
|
||||
note = self._note_with(tmp_path, "#indieweb is fun\n\n##two\n\n# Real heading")
|
||||
assert "<p>#indieweb is fun</p>" in note.html
|
||||
assert "<p>##two</p>" in note.html
|
||||
assert "<h1>Real heading</h1>" in note.html
|
||||
assert note.excerpt.startswith("#indieweb is fun ##two")
|
||||
|
||||
def test_hashtag_in_code_untouched(self, tmp_path):
|
||||
note = self._note_with(tmp_path, "```\n#include <x>\n```")
|
||||
assert "\\" not in note.html and "include" in note.html
|
||||
|
||||
def test_split_title(self):
|
||||
"""A '# Heading' first line is split off as the title"""
|
||||
assert split_title("# My Title\n\nBody") == ("My Title", "Body")
|
||||
@@ -266,22 +268,6 @@ class TestNoteModel:
|
||||
assert split_title("No heading\n# later") == (None, "No heading\n# later")
|
||||
assert split_title("#\nBody") == (None, "#\nBody")
|
||||
|
||||
def test_body_html_leaves_out_title(self, tmp_path):
|
||||
"""Pages show the title themselves, so the body must not repeat it"""
|
||||
note = self._note_with(tmp_path, "# My Title\n\nBody text.")
|
||||
assert "My Title" not in note.body_html
|
||||
assert "<p>Body text.</p>" in note.body_html
|
||||
assert "<h1>My Title</h1>" in note.html # html is unchanged
|
||||
|
||||
def test_body_html_untitled_matches_html(self, tmp_path):
|
||||
note = self._note_with(tmp_path, "Plain note.")
|
||||
assert note.body_html == note.html
|
||||
|
||||
def test_excerpt_leaves_out_title(self, tmp_path):
|
||||
"""Previews show the title above the excerpt, so it is not repeated"""
|
||||
note = self._note_with(tmp_path, "# Title\n\nShort content.")
|
||||
assert note.excerpt == "Short content."
|
||||
|
||||
def test_permalink(self):
|
||||
"""Test permalink generation"""
|
||||
note = Note(
|
||||
|
||||
@@ -68,7 +68,7 @@ def sample_notes(app):
|
||||
notes = []
|
||||
for i in range(10):
|
||||
note = create_note(
|
||||
content=f"# Test Note {i}\n\nContent for note {i}.",
|
||||
title=f"Test Note {i}", content=f"Content for note {i}.",
|
||||
published=(i < 7), # First 7 published, last 3 drafts
|
||||
)
|
||||
notes.append(note)
|
||||
@@ -316,7 +316,7 @@ class TestFeedEdgeCases:
|
||||
"""Test feed handles special characters correctly"""
|
||||
with app.app_context():
|
||||
create_note(
|
||||
content="# Test & Special <Characters>\n\n'Quotes' and \"doubles\".",
|
||||
title="Test & Special <Characters>", content="'Quotes' and \"doubles\".",
|
||||
published=True,
|
||||
)
|
||||
|
||||
@@ -330,7 +330,7 @@ class TestFeedEdgeCases:
|
||||
def test_feed_with_unicode_content(self, client, app):
|
||||
"""Test feed handles Unicode content"""
|
||||
with app.app_context():
|
||||
create_note(content="# Test Unicode 你好 🚀\n\nEmojis and ümlauts.", published=True)
|
||||
create_note(title="Test Unicode 你好 🚀", content="Emojis and ümlauts.", published=True)
|
||||
|
||||
response = client.get("/feed.xml")
|
||||
assert response.status_code == 200
|
||||
|
||||
@@ -121,6 +121,40 @@ class TestCompose:
|
||||
assert "…" in text
|
||||
|
||||
|
||||
class TestArticles:
|
||||
"""Articles are crossposted as title, teaser and link, without images (v2.16.0)"""
|
||||
|
||||
LINK = "https://blog.example.com/note/x"
|
||||
|
||||
def test_title_summary_and_link(self, app):
|
||||
with app.app_context():
|
||||
note = create_note(content="Long body " * 100, title="My Article",
|
||||
summary="The gist.", published=True)
|
||||
text = compose_text(note, self.LINK, ["IndieWeb"], 500)
|
||||
assert text == f"My Article\n\nThe gist.\n\n{self.LINK}\n\n#IndieWeb"
|
||||
|
||||
def test_without_summary_the_start_of_the_text(self, app):
|
||||
with app.app_context():
|
||||
note = create_note(content="Some *markdown* " + "word " * 100, title="T", published=True)
|
||||
title, teaser, link = compose_text(note, self.LINK, [], 5000).split("\n\n")
|
||||
assert (title, link) == ("T", self.LINK)
|
||||
assert teaser.startswith("Some markdown word") and teaser.endswith("…")
|
||||
assert len(teaser) <= 201
|
||||
|
||||
def test_edit_follows_and_no_images(self, app, server, monkeypatch):
|
||||
looked_up = []
|
||||
monkeypatch.setattr(syndication, "_media_files", lambda *a, **k: looked_up.append(a) or [])
|
||||
with app.test_request_context():
|
||||
note = create_note(content="Body", title="Before", published=True)
|
||||
select_targets(note.id, ["gotosocial"])
|
||||
process_due()
|
||||
update_note(id=note.id, title="After")
|
||||
process_due()
|
||||
assert server.statuses["1"]["status"].startswith("After\n\nBody")
|
||||
assert not server.statuses["1"].get("media_ids")
|
||||
assert looked_up == []
|
||||
|
||||
|
||||
class TestLifecycle:
|
||||
def test_publish_creates_status(self, app, server):
|
||||
note = publish(app, tags=["Test"])
|
||||
|
||||
@@ -179,12 +179,30 @@ class TestText:
|
||||
assert raw[tag["index"]["byteStart"]:tag["index"]["byteEnd"]] == b"#IndieWeb"
|
||||
assert tag["features"][0]["tag"] == "IndieWeb"
|
||||
|
||||
def test_hashtag_at_line_start_stays_a_tag(self):
|
||||
"""Not turned into a heading and lost (v2.16.0)"""
|
||||
assert plain_text("#indieweb is fun") == "#indieweb is fun"
|
||||
assert [f["features"][0]["tag"] for f in bluesky_facets(plain_text("#indieweb is fun"))] == ["indieweb"]
|
||||
|
||||
def test_fragment_in_url_is_not_a_tag(self):
|
||||
facets = bluesky_facets("see https://x.example/page#section and #42 too")
|
||||
assert [f["features"][0]["$type"] for f in facets] == ["app.bsky.richtext.facet#link"]
|
||||
|
||||
|
||||
class TestLifecycle:
|
||||
def test_article_posts_teaser_with_its_own_card(self, app, pds):
|
||||
"""An article's card is the article, even when its text links elsewhere (v2.16.0)"""
|
||||
with app.test_request_context():
|
||||
note = create_note(content="See [this](https://elsewhere.example/x) for more.",
|
||||
title="My Article", summary="The gist.", published=True)
|
||||
select_targets(note.id, ["bluesky"])
|
||||
process_due()
|
||||
record = only_record(pds)
|
||||
permalink = f"https://blog.example.com/note/{note.slug}"
|
||||
assert record["text"] == f"My Article\n\nThe gist.\n\n{permalink}"
|
||||
assert record["embed"]["external"] == {
|
||||
"uri": permalink, "title": "My Article", "description": "The gist."}
|
||||
|
||||
def test_publish_posts_with_link_card(self, app, pds):
|
||||
note = publish(app, content="Hello **Bluesky**", tags=["IndieWeb"])
|
||||
record = only_record(pds)
|
||||
|
||||
Reference in new issue
Block a user