基本的には、ブラウザウィンドウからそのHTMLをコピーして、textarea要素に貼り付けるだけの効果が必要です。
たとえば、私はこれが欲しいです:
<p>Some</p>
<div>text<br />Some</div>
<div>text</div>
これになるために:
Some
text
Some
text
基本的には、ブラウザウィンドウからそのHTMLをコピーして、textarea要素に貼り付けるだけの効果が必要です。
たとえば、私はこれが欲しいです:
<p>Some</p>
<div>text<br />Some</div>
<div>text</div>
これになるために:
Some
text
Some
text
そのHTMLがWebページ内に表示されている場合は、ユーザーを選択して(またはTextRange
、IEでのみ)表示できます。これにより、必ずしも先頭と末尾の空白でなくても、改行が保持されます。
2012年12月10日更新
ただし、オブジェクトのtoString()
メソッドはまだ標準化されておらず、ブラウザ間で一貫性のない動作をするため、このアプローチは不安定な状況に基づいており、現在は使用しないことをお勧めします。受け入れられなかった場合は、この回答を削除します。Selection
デモ: http: //jsfiddle.net/wv49v/
コード:
function getInnerText(el) {
var sel, range, innerText = "";
if (typeof document.selection != "undefined" && typeof document.body.createTextRange != "undefined") {
range = document.body.createTextRange();
range.moveToElementText(el);
innerText = range.text;
} else if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
sel = window.getSelection();
sel.selectAllChildren(el);
innerText = "" + sel;
sel.removeAllRanges();
}
return innerText;
}
しばらく前に、このために書いたコードを見つけようとしました。それはうまくいきました。それが何をしたかを概説させてください、そしてうまくいけば、あなたはその振る舞いを複製することができます。
これをさらに拡張して、順序付きリストや順序なしリストなどをフォーマットすることもできます。それは本当にあなたがどこまで行きたいかによるだけです。
編集
コードが見つかりました!
public static string Convert(string template)
{
template = Regex.Replace(template, "<img .*?alt=[\"']?([^\"']*)[\"']?.*?/?>", "$1"); /* Use image alt text. */
template = Regex.Replace(template, "<a .*?href=[\"']?([^\"']*)[\"']?.*?>(.*)</a>", "$2 [$1]"); /* Convert links to something useful */
template = Regex.Replace(template, "<(/p|/div|/h\\d|br)\\w?/?>", "\n"); /* Let's try to keep vertical whitespace intact. */
template = Regex.Replace(template, "<[A-Za-z/][^<>]*>", ""); /* Remove the rest of the tags. */
return template;
}
私はこの答えに基づいて関数を作成しました:https ://stackoverflow.com/a/42254787/3626940
function htmlToText(html){
//remove code brakes and tabs
html = html.replace(/\n/g, "");
html = html.replace(/\t/g, "");
//keep html brakes and tabs
html = html.replace(/<\/td>/g, "\t");
html = html.replace(/<\/table>/g, "\n");
html = html.replace(/<\/tr>/g, "\n");
html = html.replace(/<\/p>/g, "\n");
html = html.replace(/<\/div>/g, "\n");
html = html.replace(/<\/h>/g, "\n");
html = html.replace(/<br>/g, "\n"); html = html.replace(/<br( )*\/>/g, "\n");
//parse html into text
var dom = (new DOMParser()).parseFromString('<!doctype html><body>' + html, 'text/html');
return dom.body.textContent;
}
chrmcpnの回答に基づいて、node.jsのビルドスクリプトの一部として、基本的なHTMLメールテンプレートをプレーンテキストバージョンに変換する必要がありました。JSDOMを使用して機能させる必要がありましたが、コードは次のとおりです。
const htmlToText = (html) => {
html = html.replace(/\n/g, "");
html = html.replace(/\t/g, "");
html = html.replace(/<\/p>/g, "\n\n");
html = html.replace(/<\/h1>/g, "\n\n");
html = html.replace(/<br>/g, "\n");
html = html.replace(/<br( )*\/>/g, "\n");
const dom = new JSDOM(html);
let text = dom.window.document.body.textContent;
text = text.replace(/ /g, "");
text = text.replace(/\n /g, "\n");
text = text.trim();
return text;
}
3つのステップ。
First get the html as a string.
Second, replace all <BR /> and <BR> with \r\n.
Third, use the regular expression "<(.|\n)*?>" to replace all markup with "".