EJS - 2: Using EJS in 'a' and 'img' in Gridea

2020-01-24 Views EJS537字4 min read
featureimg

This passage shows how to use EJS to add referrerpolicy="no-referrer" to 'img', and set 'rel' and 'target' in 'a' in Gridea.

Please notice that the following code is used in Gridea. But if you use another software to render HTML by using EJS, you can still use the following code.

<%- Content();%><%  -%>
<%
function refineContent(str){
   indexC = str.indexOf('<img');
   while(indexC != -1){
		indexEnd = str.indexOf('>', indexC);
		str = str.substring(0,indexEnd) + ' referrerpolicy="no-referrer"' + str.substring(indexEnd);
		indexC = str.indexOf('<img', indexEnd);
   }
   indexC = str.indexOf('<a');
   while(indexC != -1){
		indexEnd = str.indexOf('>', indexC);
		str = str.substring(0,indexEnd) + ' rel="external nofollow" target="_blank"' + str.substring(indexEnd);
		indexC = str.indexOf('<a', indexEnd);
   }
   return str;
}
function Content(){
    let con= post.content;
    con = refineContent(con);
    return con;
}
%>

If you want to implement other js code in local environment, you can use js at the following way:

<%- <FunctionName()>%>
<%
function FunctionName(){
    //Your code here
}
%>

When you want to implement some of your js locally, you need to know these:

  • Ensure that the js is not used to enhance user experience when visiting your website
  • Ensure that the effect of js is the same for all user (EG: if you want to set a random background color when people visit your website, you cannot implement the code locally)

I do offer another js code in EJS:

function refineContent(str){
   indexC = str.indexOf('<img');
   strTemp = "";
   enableReference = 0;
   while(indexC != -1){
		indexEnd = str.indexOf('>', indexC);
		str = str.substring(0,indexEnd) + ' referrerpolicy="no-referrer"' + str.substring(indexEnd);
		indexC = str.indexOf('<img', indexEnd);
   }
   indexC = str.indexOf('<a');
   while(indexC != -1){
		indexEnd = str.indexOf('>', indexC);
		indexTitle = str.indexOf('title', indexC);
		if (indexTitle < indexEnd && indexTitle != -1){
			indexTitleEnd = str.indexOf('"', indexTitle + 7);
			title = str.substring(indexTitle + 7, indexTitleEnd);
			indexLink = str.indexOf('href', indexC);
			indexLinkEnd = str.indexOf('"', indexLink + 6);
			link = str.substring(indexLink + 6, indexLinkEnd);
			if (enableReference == 0){
				str = str + '<h2>References</h2>'
				enableReference = 1;
			}
			strTemp += '<div style="display: inline; width: 10%; background: none; font-size: 80%; opacity: 0.6; line-height: 26px;">[' + String(enableReference) + ']</div>&nbsp&nbsp<p style="display: inline; font-size: 14px; width: 90%; padding: 0px; margin: 0; line-height: 26px; color: black; word-break: break-all; width: calc(100%-50);">'+title+':<em style="font-style: italic; color: black;">&nbsp<a href="'+link+'" target="_blank" style="text-decoration: none; color: #1e6bb8; word-wrap: break-word; font-weight: bold; border-bottom: 1px solid #1e6bb8;">'+link+'</a></em></p><br>';
			enableReference = enableReference + 1;
		}
		indexC = str.indexOf('<a', indexEnd);
   }
   str += strTemp + '</span>'
   return str;
}
function Content(){
    let con= post.content;
    con = refineContent(con+'<span id="footnote">');
    return con;
}
%>

This code is used to automatically generate references at the end of the passage. The effect and the original js code can be seen at this page


Big Image
EOF
本文章采用CC BY-NC-SA 4.0进行许可。转载请注明出处!
上一篇

囧妈


下一篇

Data Structure - linkedList