Crap, lets use GH html instead

This commit is contained in:
shamoon committed 2026-11-18 08:41:39 -08:00
1 parent a1a2eb5b36
commit 084a3b1cb8
9 files changed
+87 -119

No files matched your search

@@ -25,7 +25,7 @@
@for (release of releases(); track release.version) {
<section class="release-notes" [class.mt-4]="!$first">
<h5>{{ release.version }}</h5>
<div class="release-notes-body" [innerHTML]="release.body | markdown: false"></div>
<div class="release-notes-body" [innerHTML]="release.body_html"></div>
</section>
}
}
@@ -1,6 +1,39 @@
:host ::ng-deep .release-notes-body > h3 {
font-size: 1rem;
font-weight: 600;
margin-top: 1rem;
margin-bottom: 0.5rem;
// Styles for GitHub-rendered release notes (body_html)
:host ::ng-deep .release-notes-body {
// Repeats the version heading
> h2:first-child {
display: none;
}
> h3 {
font-size: 1rem;
font-weight: 600;
margin-top: 1rem;
margin-bottom: 0.5rem;
}
details {
margin-bottom: 1rem;
> summary {
color: var(--bs-secondary-color);
cursor: pointer;
margin-bottom: 0.5rem;
}
}
.markdown-alert {
border-left: 0.25rem solid var(--bs-info);
padding: 0.5rem 1rem;
margin-bottom: 1rem;
> :last-child {
margin-bottom: 0;
}
}
.markdown-alert-title {
font-weight: 600;
margin-bottom: 0.25rem;
}
}
@@ -8,10 +8,7 @@ import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
import { SETTINGS_KEYS } from 'src/app/data/ui-settings'
import { SettingsService } from 'src/app/services/settings.service'
import { environment } from 'src/environments/environment'
import {
cleanReleaseBody,
ReleaseNotesDialogComponent,
} from './release-notes-dialog.component'
import { ReleaseNotesDialogComponent } from './release-notes-dialog.component'
describe('ReleaseNotesDialogComponent', () => {
let component: ReleaseNotesDialogComponent
@@ -58,7 +55,9 @@ describe('ReleaseNotesDialogComponent', () => {
{
version: '3.5.1',
name: 'Paperless-ngx v3.5.1',
body: '## paperless-ngx 3.5.1\r\n\r\n### Bug Fixes\r\n\r\n- Fix: a thing ([#1](https://github.com/paperless-ngx/paperless-ngx/pull/1))',
body_html:
'<h2>paperless-ngx 3.5.1</h2><h3>Bug Fixes</h3><ul><li>Fix: a thing</li></ul>' +
'<h3>All App Changes</h3><details><summary>1 change</summary><ul><li>Fix: a thing</li></ul></details>',
html_url: '',
published_at: '',
},
@@ -69,35 +68,10 @@ describe('ReleaseNotesDialogComponent', () => {
const text = fixture.nativeElement.textContent
expect(text).toContain('Updated from 3.4.2 to 3.5.1')
expect(text).toContain('Bug Fixes')
expect(text).not.toContain('paperless-ngx 3.5.1')
expect(text).not.toContain('(github.com)')
})
it('should drop the duplicate All App Changes section and details markup', () => {
const body = [
'## paperless-ngx 3.5.1',
'### Bug Fixes',
'- Fix: a thing',
'### Dependencies',
'<details>',
'<summary>1 change</summary>',
'',
'- Chore(deps): bump a thing',
'</details>',
'### All App Changes',
'<details>',
'<summary>2 changes</summary>',
'',
'- Fix: a thing',
'- Chore(deps): bump a thing',
'</details>',
].join('\r\n')
const cleaned = cleanReleaseBody(body)
expect(cleaned).toContain('### Dependencies')
expect(cleaned).toContain('- Chore(deps): bump a thing')
expect(cleaned).not.toContain('All App Changes')
expect(cleaned).not.toContain('details>')
expect(cleaned).not.toContain('summary>')
expect(
fixture.nativeElement.querySelector('.release-notes-body details summary')
.textContent
).toEqual('1 change')
})
it('should show a message if release notes could not be loaded', () => {
@@ -3,7 +3,6 @@ import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
import { first } from 'rxjs'
import { BootstrapIconComponent } from 'src/app/components/common/bootstrap-icon/bootstrap-icon.component'
import { SETTINGS_KEYS } from 'src/app/data/ui-settings'
import { MarkdownPipe } from 'src/app/pipes/markdown.pipe'
import {
AppRelease,
RemoteVersionService,
@@ -12,25 +11,11 @@ import { SettingsService } from 'src/app/services/settings.service'
const RELEASES_URL = 'https://github.com/paperless-ngx/paperless-ngx/releases'
const RELEASE_TITLE = /^\s*##\s+paperless-ngx[^\n]*\n+/i
const ALL_CHANGES_SECTION = /^###\s+All App Changes\s*$[\s\S]*/im
const DETAILS_MARKUP = /^\s*(<\/?details>|<summary>.*<\/summary>)\s*$/gim
/**
* Drops the "## paperless-ngx x.y.z" title, the "All App Changes"
* section and `<details>` / `<summary>` wrappers.
*/
export function cleanReleaseBody(body: string): string {
body = body.replace(RELEASE_TITLE, '')
body = body.replace(ALL_CHANGES_SECTION, '')
return body.replace(DETAILS_MARKUP, '').trim()
}
@Component({
selector: 'pngx-release-notes-dialog',
templateUrl: './release-notes-dialog.component.html',
styleUrls: ['./release-notes-dialog.component.scss'],
imports: [BootstrapIconComponent, MarkdownPipe],
imports: [BootstrapIconComponent],
})
export class ReleaseNotesDialogComponent {
readonly activeModal = inject(NgbActiveModal)
@@ -63,12 +48,7 @@ export class ReleaseNotesDialogComponent {
.pipe(first())
.subscribe({
next: (notes) => {
this.releases.set(
notes.releases.map((release) => ({
...release,
body: cleanReleaseBody(release.body),
}))
)
this.releases.set(notes.releases)
this.loading.set(false)
},
error: () => {
@@ -87,10 +87,4 @@ describe('MarkdownPipe', () => {
)
expect(html).not.toMatch(/"\s*onmouseover=/)
})
it('should omit link hostnames when requested', () => {
const html = pipe.transform('[PR](https://github.com/x/y/pull/1)', false)
expect(html).toContain('href="https://github.com/x/y/pull/1"')
expect(html).not.toContain('(github.com)')
})
})
+28 -46
View File
@@ -12,59 +12,41 @@ function allowedUrl(href: string): URL | null {
}
}
/**
* Treat content as untrusted: no raw HTML, no images, and only
* absolute links. All escaping is done by marked's default renderer, and
* Angular's sanitizer should run on top of this as well.
*/
function buildRenderer(showLinkHosts: boolean): Partial<Renderer> {
return {
html(this: Renderer, { raw, text }: Tokens.HTML | Tokens.Tag): string {
return this.text({ type: 'text', raw, text })
},
image(this: Renderer, { raw, text }: Tokens.Image): string {
return this.text({ type: 'text', raw, text })
},
link(this: Renderer, token: Tokens.Link): string {
const url = allowedUrl(token.href)
if (!url) return this.parser.parseInline(token.tokens)
const anchor = Renderer.prototype.link
.call(this, token)
.replace(
'<a ',
'<a target="_blank" rel="noopener noreferrer nofollow" '
)
if (!showLinkHosts || !url.hostname) return anchor
const hostname = this.text({ type: 'text', raw: '', text: url.hostname })
return `${anchor} <span class="text-muted small">(${hostname})</span>`
},
}
// Treat chat content as untrusted: no raw HTML, no images, and only
// absolute links. All escaping is done by marked's default renderer, and
// Angular's sanitizer runs on top of this as well.
const renderer: Partial<Renderer> = {
html(this: Renderer, { raw, text }: Tokens.HTML | Tokens.Tag): string {
return this.text({ type: 'text', raw, text })
},
image(this: Renderer, { raw, text }: Tokens.Image): string {
return this.text({ type: 'text', raw, text })
},
link(this: Renderer, token: Tokens.Link): string {
const url = allowedUrl(token.href)
if (!url) return this.parser.parseInline(token.tokens)
const anchor = Renderer.prototype.link
.call(this, token)
.replace('<a ', '<a target="_blank" rel="noopener noreferrer nofollow" ')
if (!url.hostname) return anchor
const hostname = this.text({ type: 'text', raw: '', text: url.hostname })
return `${anchor} <span class="text-muted small">(${hostname})</span>`
},
}
function buildMarked(showLinkHosts: boolean): Marked {
return new Marked({
async: false,
gfm: true,
breaks: true,
renderer: buildRenderer(showLinkHosts),
})
}
const markdown = buildMarked(true)
const markdownWithoutLinkHosts = buildMarked(false)
const markdown = new Marked({
async: false,
gfm: true,
breaks: true,
renderer,
})
@Pipe({
name: 'markdown',
})
export class MarkdownPipe implements PipeTransform {
/**
* @param showLinkHosts append each link's hostname, e.g. "(example.com)".
* Can be disabled for trusted content where every link points to the same host.
*/
transform(value: string | null | undefined, showLinkHosts = true): string {
transform(value: string | null | undefined): string {
if (!value) return ''
return (showLinkHosts ? markdown : markdownWithoutLinkHosts).parse(
value
) as string
return markdown.parse(value) as string
}
}
@@ -11,7 +11,7 @@ export interface AppRemoteVersion {
export interface AppRelease {
version: string
name: string
body: string
body_html: string
html_url: string
published_at: string
}