-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: internal links and external work with ckeditor footnotes (#2084)
- Loading branch information
1 parent
27165aa
commit 58676b9
Showing
14 changed files
with
128 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
libs/vre/resource-editor/resource-properties/src/lib/footnote-parser.pipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; | ||
import { FootnoteService } from './footnote.service'; | ||
|
||
@Pipe({ | ||
name: 'footnoteParser', | ||
}) | ||
export class FootnoteParserPipe implements PipeTransform { | ||
private readonly _footnoteRegExp = /<footnote content="([^>]+)">([^<]*)<\/footnote>/g; | ||
|
||
constructor( | ||
private _sanitizer: DomSanitizer, | ||
private _footnoteService: FootnoteService | ||
) {} | ||
|
||
transform(value: null | string): SafeHtml | null { | ||
if (value === null) { | ||
return value; // Return as is if value is empty or null | ||
} // does nothing if only displayMode changes | ||
|
||
if (!this._containsFootnote(value)) { | ||
return this._sanitizer.bypassSecurityTrustHtml(value); | ||
} | ||
|
||
return this._parseFootnotes(value); | ||
} | ||
|
||
private _containsFootnote(text: string) { | ||
return text.match(this._footnoteRegExp) !== null; | ||
} | ||
|
||
private _parseFootnotes(controlValue: string): SafeHtml { | ||
const matches = controlValue.matchAll(this._footnoteRegExp); | ||
let newValue = controlValue; | ||
if (matches) { | ||
Array.from(matches).forEach(matchArray => { | ||
const uuid = window.crypto.getRandomValues(new Uint32Array(1))[0].toString(); | ||
const parsedFootnote = `<footnote content="${matchArray[1]}" id="${uuid}">${this._footnoteService.footnotes.length + 1}</footnote>`; | ||
newValue = newValue.replace(matchArray[0], parsedFootnote); | ||
this._footnoteService.addFootnote( | ||
uuid, | ||
this._sanitizer.bypassSecurityTrustHtml(this._unescapeHtml(this._unescapeHtml(matchArray[1]))) | ||
); | ||
}); | ||
} | ||
return this._sanitizer.bypassSecurityTrustHtml(this._unescapeHtml(newValue)); | ||
} | ||
|
||
private _unescapeHtml(str: string) { | ||
const unescapeMap = { | ||
'&': '&', | ||
'<': '<', | ||
'>': '>', | ||
'"': '"', | ||
''': "'", | ||
}; | ||
return str.replace(/&(amp|lt|gt|quot|#039);/g, match => unescapeMap[match]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; | ||
|
||
@Pipe({ | ||
name: 'addTargetBlank', | ||
}) | ||
export class AddTargetBlankPipe implements PipeTransform { | ||
constructor(private sanitizer: DomSanitizer) {} | ||
|
||
transform(value: null | string | SafeHtml): SafeHtml | null { | ||
if (value === null) { | ||
return value; // Return as is if value is empty or null | ||
} | ||
|
||
const htmlString = typeof value === 'string' ? value : value['changingThisBreaksApplicationSecurity']; | ||
|
||
// Create a temporary DOM element to manipulate the HTML string | ||
const tempDiv = document.createElement('div'); | ||
tempDiv.innerHTML = htmlString; | ||
|
||
// Find all <a> tags and add `target="_blank"` if it doesn't exist | ||
const links = tempDiv.querySelectorAll('a'); | ||
links.forEach((link: HTMLAnchorElement) => { | ||
if (!link.hasAttribute('target')) { | ||
link.setAttribute('target', '_blank'); | ||
} | ||
}); | ||
|
||
// Return the modified HTML string | ||
return this.sanitizer.bypassSecurityTrustHtml(tempDiv.innerHTML); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters