Skip to content

Commit 983fe70

Browse files
committed
HTMLView: add paragraph break hints for previews
1 parent ae7b991 commit 983fe70

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

helpers/HTMLView.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ export async function getNoteContentAsHTML(content: string, note: TNote): Promis
169169
}
170170
}
171171

172+
lines = addParagraphBreakHints(lines)
173+
172174
// Make this proper Markdown -> HTML via showdown library
173175
// Set some options to turn on various more advanced HTML conversions (see actual code at https://github.com/showdownjs/showdown/blob/master/src/options.js#L109):
174176
const converterOptions = {
@@ -1157,6 +1159,44 @@ export function convertHighlightsToHTML(input: string): string {
11571159
return output
11581160
}
11591161

1162+
function addParagraphBreakHints(lines: Array<string>): Array<string> {
1163+
const output: Array<string> = []
1164+
for (let i = 0; i < lines.length; i++) {
1165+
const currentLine = lines[i]
1166+
output.push(currentLine)
1167+
1168+
const trimmedCurrent = currentLine.trim()
1169+
if (trimmedCurrent === '') {
1170+
continue
1171+
}
1172+
1173+
const nextLine = lines[i + 1]
1174+
if (nextLine == null) {
1175+
continue
1176+
}
1177+
const trimmedNext = nextLine.trim()
1178+
if (trimmedNext === '') {
1179+
continue
1180+
}
1181+
1182+
const currentIsList = isListLine(trimmedCurrent)
1183+
const nextIsList = isListLine(trimmedNext)
1184+
1185+
if ((currentIsList && !nextIsList) || isIsolatedWikiLink(trimmedCurrent)) {
1186+
output.push('')
1187+
}
1188+
}
1189+
return output
1190+
}
1191+
1192+
function isListLine(line: string): boolean {
1193+
return /^([*\-+]|\d+\.)\s+/.test(line)
1194+
}
1195+
1196+
function isIsolatedWikiLink(line: string): boolean {
1197+
return /^\[\[[^\]]+\]\]$/.test(line)
1198+
}
1199+
11601200
function convertWikiLinksToHTML(input: string): string {
11611201
return input.replace(/\[\[(.+?)\]\]/g, (_match, content) => {
11621202
const { url, displayText } = buildNotePlanLinkFromWikiContent(content)

helpers/__tests__/HTMLView.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,38 @@ describe('getNoteContentAsHTML()', () => {
226226
`<a class="internal-note-link" href="${expectedUrl}">Rehearsal Setup for Playback Tracks</a>`,
227227
)
228228
})
229+
230+
test('keeps wiki-link paragraph separate from following text', async () => {
231+
const targetNote = {
232+
filename: 'Library/Example.md',
233+
title: 'Example',
234+
}
235+
DataStore.projectNoteByTitle = jest.fn().mockReturnValue([targetNote])
236+
DataStore.projectNoteByFilename = jest.fn().mockReturnValue(targetNote)
237+
238+
const result = await h.getNoteContentAsHTML('[[Example]]\nFollow up text', {
239+
filename: 'Projects/Test/Test Note.md',
240+
title: 'Test Note',
241+
type: 'Notes',
242+
})
243+
244+
expect(result).toContain('<p>Follow up text</p>')
245+
expect(result).not.toMatch(/<a class="internal-note-link"[^>]*>Example<\/a><br \/>Follow up text/)
246+
})
247+
248+
test('ensures text following a list is not captured inside the list item', async () => {
249+
const note = {
250+
filename: 'Projects/Test/Test Note.md',
251+
title: 'Test Note',
252+
type: 'Notes',
253+
}
254+
255+
const markdown = '- Item one\n- Item two\nFollow up text'
256+
const result = await h.getNoteContentAsHTML(markdown, note)
257+
258+
expect(result).toContain('<p>Follow up text</p>')
259+
expect(result).not.toMatch(/<li>[^<]*Follow up text/)
260+
})
229261
})
230262

231263
/*

np.Preview/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Arbitrary elevation to 1.0.0
88
### General HTML Helper Change
99
- convert `[[wikilinks]]` in HTML output to be real NotePlan callback URLs rather than just underlined text that does nothing
1010
- fix bug in showdown relative to NP to ensure horizontal rules have a blank line before them so they render properly
11+
- add paragraph break hints so isolated wikilinks and text following lists render in their own paragraphs
1112

1213
### Preview-specific Changes
1314
- move preview-specific spacing and typography to `previewStyles` file, with:

0 commit comments

Comments
 (0)