GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #4613 from developing-today/queue/6492-1307-plain-… · developing-today/code@1155872

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pkgs/id/src/web
    • markdown.rs
    • routes.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pkgs/id/src/web/markdown.rs‎

Copy file name to clipboardExpand all lines: pkgs/id/src/web/markdown.rs
+24-3Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5241,11 +5241,32 @@ fn build_mark_chain<'a>(
52415241

52425242
/// Convert plain text to `ProseMirror` JSON document.
52435243
///
5244-
/// Each line becomes a paragraph.
5244+
/// Each line becomes a paragraph. The split is on the newline
5245+
/// character (not `str::lines`), so a trailing newline yields a final
5246+
/// empty paragraph and the save side (`extract_plain_text`, which joins
5247+
/// paragraphs with a newline) reproduces it exactly: no-edit open/save
5248+
/// is byte-stable for the trailing newline (#1307). A carriage return
5249+
/// left by a CRLF ending is stripped, but only from segments the newline
5250+
/// actually terminated: a lone trailing CR is content and survives. CRLF
5251+
/// files are normalized to LF on save, with their trailing-newline state
5252+
/// preserved (Raw mode stays byte-exact).
52455253
#[must_use]
52465254
pub fn plain_text_to_prosemirror(text: &str) -> Value {
5247-
let paragraphs: Vec<Value> = text
5248-
.lines()
5255+
let segments: Vec<&str> = text.split('\n').collect();
5256+
let last = segments.len() - 1;
5257+
let paragraphs: Vec<Value> = segments
5258+
.into_iter()
5259+
.enumerate()
5260+
.map(|(i, line)| {
5261+
// Only a segment actually terminated by '\n' can carry the
5262+
// CR of a CRLF ending; the final segment keeps a lone
5263+
// trailing CR verbatim (Codex finding on PR #4613).
5264+
if i < last {
5265+
line.strip_suffix('\r').unwrap_or(line)
5266+
} else {
5267+
line
5268+
}
5269+
})
52495270
.map(|line| {
52505271
if line.is_empty() {
52515272
json!({"type": "paragraph"})
Collapse file

‎pkgs/id/src/web/routes.rs‎

Copy file name to clipboardExpand all lines: pkgs/id/src/web/routes.rs
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,13 @@ fn convert_doc_to_bytes(name: &str, doc: &serde_json::Value) -> Result<Vec<u8>,
22212221
/// dropping it. The pre-#1306 version handled only top-level
22222222
/// paragraphs/headings and silently deleted everything else on save.
22232223
///
2224+
/// Line endings: plain-mode load (`plain_text_to_prosemirror`) strips
2225+
/// the CR of CRLF endings and this join emits LF only, so a CRLF file
2226+
/// is normalized to LF on save - a deliberate, documented choice
2227+
/// (#1307), pinned by the round-trip tests below; the file's
2228+
/// trailing-newline state is preserved through the load/save pair.
2229+
/// Byte-exact CRLF round-trips remain available in Raw mode.
2230+
///
22242231
/// Inline atoms carry no `content`, so recursion alone loses them.
22252232
/// Pinned representations (#1491): an `image` contributes its `alt`
22262233
/// text verbatim when non-empty, otherwise its `src` verbatim when
@@ -3332,6 +3339,40 @@ mod tests {
33323339
serde_json::json!({"type": "paragraph", "content": [{"type": "text", "text": text}]})
33333340
}
33343341

3342+
#[test]
3343+
fn test_plain_mode_roundtrip_preserves_trailing_newline() {
3344+
// #1307 pinned policy: the real load/save pair is byte-stable
3345+
// for the common shapes, including the trailing newline.
3346+
for text in ["a\nb\n", "a\nb", "a\n", "\n", ""] {
3347+
let doc = super::super::markdown::plain_text_to_prosemirror(text);
3348+
assert_eq!(extract_plain_text(&doc), text, "{text:?}");
3349+
}
3350+
}
3351+
3352+
#[test]
3353+
fn test_plain_mode_roundtrip_normalizes_crlf_to_lf() {
3354+
// #1307 pinned policy: CRLF normalizes to LF on save, and the
3355+
// trailing-newline state survives the normalization.
3356+
let doc = super::super::markdown::plain_text_to_prosemirror("alpha\r\nbeta\r\n");
3357+
assert_eq!(extract_plain_text(&doc), "alpha\nbeta\n");
3358+
let doc = super::super::markdown::plain_text_to_prosemirror("alpha\r\nbeta");
3359+
assert_eq!(extract_plain_text(&doc), "alpha\nbeta");
3360+
}
3361+
3362+
#[test]
3363+
fn test_plain_mode_roundtrip_preserves_lone_terminal_cr() {
3364+
// Codex finding on PR #4613: a carriage return that is NOT part
3365+
// of a CRLF ending is content. Only segments terminated by '\n'
3366+
// may have their CR stripped; the final segment keeps it.
3367+
let doc = super::super::markdown::plain_text_to_prosemirror("alpha\r");
3368+
assert_eq!(extract_plain_text(&doc), "alpha\r");
3369+
let doc = super::super::markdown::plain_text_to_prosemirror("alpha\r\nbeta\r");
3370+
assert_eq!(extract_plain_text(&doc), "alpha\nbeta\r");
3371+
// A CR-only line ending mid-file is likewise preserved.
3372+
let doc = super::super::markdown::plain_text_to_prosemirror("alpha\rbeta\n");
3373+
assert_eq!(extract_plain_text(&doc), "alpha\rbeta\n");
3374+
}
3375+
33353376
#[test]
33363377
fn test_extract_plain_text_paragraph_heading_byte_identical() {
33373378
// Pre-#1306 behaviour for paragraph/heading-only docs is unchanged.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.