mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-05 05:42:39 +02:00
refactor: improve Markdown fence handling in report generation and viewer
This commit is contained in:
parent
f7bbce098b
commit
8ae37bdccf
2 changed files with 16 additions and 7 deletions
|
|
@ -43,6 +43,10 @@ logger = logging.getLogger(__name__)
|
||||||
# Reusable formatting instructions appended to section-level and review prompts.
|
# Reusable formatting instructions appended to section-level and review prompts.
|
||||||
|
|
||||||
_FORMATTING_RULES = """\
|
_FORMATTING_RULES = """\
|
||||||
|
- IMPORTANT: Output raw Markdown directly. Do NOT wrap the entire output in a \
|
||||||
|
code fence (e.g. ```markdown, ````markdown, or any backtick fence). Individual \
|
||||||
|
code examples and diagrams inside the report should still use fenced code blocks, \
|
||||||
|
but the report itself must NOT be enclosed in one.
|
||||||
- Maintain proper Markdown formatting throughout.
|
- Maintain proper Markdown formatting throughout.
|
||||||
- When including code examples, ALWAYS format them as proper fenced code blocks \
|
- When including code examples, ALWAYS format them as proper fenced code blocks \
|
||||||
with the correct language identifier (e.g. ```java, ```python). Code inside code \
|
with the correct language identifier (e.g. ```java, ```python). Code inside code \
|
||||||
|
|
@ -188,16 +192,20 @@ def _strip_wrapping_code_fences(text: str) -> str:
|
||||||
|
|
||||||
Handles patterns like:
|
Handles patterns like:
|
||||||
```markdown\\n...content...\\n```
|
```markdown\\n...content...\\n```
|
||||||
|
````markdown\\n...content...\\n````
|
||||||
```md\\n...content...\\n```
|
```md\\n...content...\\n```
|
||||||
```\\n...content...\\n```
|
```\\n...content...\\n```
|
||||||
```json\\n...content...\\n```
|
```json\\n...content...\\n```
|
||||||
|
Supports 3 or more backticks (LLMs escalate when content has triple-backtick blocks).
|
||||||
"""
|
"""
|
||||||
stripped = text.strip()
|
stripped = text.strip()
|
||||||
# Match opening fence with optional language tag
|
# Match opening fence with 3+ backticks and optional language tag
|
||||||
m = re.match(r"^```(?:markdown|md|json)?\s*\n", stripped)
|
m = re.match(r"^(`{3,})(?:markdown|md|json)?\s*\n", stripped)
|
||||||
if m and stripped.endswith("```"):
|
if m:
|
||||||
|
fence = m.group(1) # e.g. "```" or "````"
|
||||||
|
if stripped.endswith(fence):
|
||||||
stripped = stripped[m.end() :] # remove opening fence
|
stripped = stripped[m.end() :] # remove opening fence
|
||||||
stripped = stripped[:-3].rstrip() # remove closing fence
|
stripped = stripped[: -len(fence)].rstrip() # remove closing fence
|
||||||
return stripped
|
return stripped
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,9 @@ interface MarkdownViewerProps {
|
||||||
*/
|
*/
|
||||||
function stripOuterMarkdownFence(content: string): string {
|
function stripOuterMarkdownFence(content: string): string {
|
||||||
const trimmed = content.trim();
|
const trimmed = content.trim();
|
||||||
const match = trimmed.match(/^```(?:markdown|md)?\s*\n([\s\S]+?)\n```\s*$/);
|
// Match 3+ backtick fences (LLMs escalate to 4+ when content has triple-backtick blocks)
|
||||||
return match ? match[1] : content;
|
const match = trimmed.match(/^(`{3,})(?:markdown|md)?\s*\n([\s\S]+?)\n\1\s*$/);
|
||||||
|
return match ? match[2] : content;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue