Skip to content

Commit

Permalink
fix: correction in markdown table alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Fernando Gomez Flores committed Nov 30, 2023
1 parent 110e879 commit 55f244a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
12 changes: 12 additions & 0 deletions libs/markdown/src/lib/markdown.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -631,5 +631,17 @@
margin: 0 0.2em 0.25em -1.6em;
vertical-align: middle;
}

td.markdown-align-center {
text-align: center;
}

td.markdown-align-left {
text-align: left;
}

td.markdown-align-right {
text-align: right;
}
}
}
26 changes: 25 additions & 1 deletion libs/markdown/src/lib/markdown.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ function addIdsToHeadings(html: string): string {
return html;
}

function changeStyleAlignmentToClass(html: string) {
if (html) {
const document: Document = new DOMParser().parseFromString(
html,

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

DOM text
is reinterpreted as HTML without escaping meta-characters.
DOM text
is reinterpreted as HTML without escaping meta-characters.
'text/html'
);
['right', 'center', 'left'].forEach((alignment: string) => {
document
.querySelectorAll(`[style*='text-align:${alignment}']`)
.forEach((style: Element) => {
style.setAttribute('class', `markdown-align-${alignment}`);
});
});

return new XMLSerializer().serializeToString(document);
}

return html;
}

@Component({
selector: 'td-markdown',
styleUrls: ['./markdown.component.scss'],
Expand Down Expand Up @@ -328,8 +348,12 @@ export class TdMarkdownComponent
// to parse the string into DOM element for now.
const div: HTMLDivElement = this._renderer.createElement('div');
this._renderer.appendChild(this._elementRef.nativeElement, div);

const html: string =
this._domSanitizer.sanitize(SecurityContext.HTML, markupStr) ?? '';
this._domSanitizer.sanitize(
SecurityContext.HTML,
changeStyleAlignmentToClass(markupStr)
) ?? '';
const htmlWithAbsoluteHrefs: string = normalizeHtmlHrefs(
html,
this._hostedUrl
Expand Down

0 comments on commit 55f244a

Please sign in to comment.