File tree Expand file tree Collapse file tree 1 file changed +32
-5
lines changed
Expand file tree Collapse file tree 1 file changed +32
-5
lines changed Original file line number Diff line number Diff line change @@ -99,20 +99,47 @@ res.send(`
9999
100100 <div class="comment-section">
101101 <h3>Comments</h3>
102- ${ wiki . comments . slice ( - 7 ) . map ( comment => {
103- return `
102+ const htmlOutput = wiki.comments.slice(-7).map(comment => {
103+ return `
104104 < div class = "comment" id = "comment-${comment.id}" >
105- <div class="comment-author">${ comment . author } <small>(${ new Date ( comment . createdAt ) . toLocaleString ( ) } )</small></div>
106- <div class="comment-content">${ comment . content } </div>
105+ < div class = "comment-author" >
106+ ${ comment . author } < small > (${ new Date ( comment . createdAt ) . toLocaleString ( ) } )</ small >
107+ </ div >
108+ < div class = "comment-content" > $ { escapeHtml ( comment . content ) } < / d i v >
107109 ${comment . replies . length > 0 ? comment . replies . map ( reply => {
108110 return `
109111 <div class="comment-reply">
110- <strong>${ reply . author } </strong>: ${ reply . content } <small>(${ new Date ( reply . createdAt ) . toLocaleString ( ) } )</small>
112+ <strong>${ reply . author } </strong>: ${ escapeHtml ( reply . content ) }
113+ <small>(${ new Date ( reply . createdAt ) . toLocaleString ( ) } )</small>
111114 </div>
112115 ` ;
113116 } ) . join ( '' ) : '' }
114117 </div >
115118 `;
119+ }).join('');
120+
121+ // Use the escapeHtml function to safely escape user input
122+ function escapeHtml(str) {
123+ return str.replace(/[&<>"'` ] / g , function ( match ) {
124+ const escape = {
125+ '&' : '&' ,
126+ '<' : '<' ,
127+ '>' : '>' ,
128+ '"' : '"' ,
129+ "'" : ''' ,
130+ "`" : '`'
131+ } ;
132+ return escape [ match ] ;
133+ } ) ;
134+ }
135+
136+ // Output the HTML
137+ console . log ( htmlOutput ) ;
138+
139+ `;
140+ }).join('') : ''}
141+ </div>
142+ ` ;
116143} ) . join ( '' ) } )
117144 </div >
118145 `;
You can’t perform that action at this time.
0 commit comments