Hướng dẫn convert text to html nodejs - chuyển văn bản sang html nodejs

Tôi đang sử dụng Nodemailer để gửi thư từ máy chủ nút. Tôi đang nhận được nội dung cho thư này từ MSSQL SQL Server được định dạng ở định dạng văn bản thuần túy, có nghĩa là có các ký tự dòng mới trong đó, tuy nhiên khi tôi gửi nó bằng Nodemailer, các ký tự mới bị thiếu và toàn bộ văn bản trông bị rối. Một cách khác là chèn các thẻ HTML để phá vỡ dòng trong văn bản thuần túy và gửi điều này hoạt động tốt. Nhưng có quá nhiều công việc có sẵn liên quan đến những gì tôi đang tìm kiếm là cho một thư viện hoặc tiện ích có thể chuyển đổi văn bản thuần túy thành HTML mà tôi có thể gửi bằng thư.

Có tự do nào cho yêu cầu này hoặc một cách để làm điều này tự động không?

hỏi ngày 18 tháng 7 năm 2017 lúc 10:02Jul 18, 2017 at 10:02

Hướng dẫn convert text to html nodejs - chuyển văn bản sang html nodejs

Sau đây sẽ bao bọc tất cả các phần được phân tách bằng nhiều dòng mới trong các đoạn (

Title

First line.
Second line.

Footer
3) và chèn ngắt (
Title

First line.
Second line.

Footer
4) khi chỉ có một dòng mới. Một khối văn bản không có bất kỳ dòng mới nào sẽ được bọc trong một đoạn văn.

template = '

' + template.replace(/\n{2,}/g, '

').replace(/\n/g, '
') + '

';

Vì vậy, ví dụ, nó sẽ lấy cái này:

Title

First line.
Second line.

Footer

Và chuyển đổi nó thành điều này:

Title

First line.
Second line.

Footer

Đã trả lời ngày 13 tháng 8 năm 2017 lúc 9:31Aug 13, 2017 at 9:31

Joxjoxjox

2.06821 huy hiệu bạc31 huy hiệu đồng21 silver badges31 bronze badges

Giải pháp đơn giản nhất là bạn có thể thay thế các ký tự dòng mới bằng

Title

First line.
Second line.

Footer
4.

Thử

text.split('\n').join('\n
\n')

Sau đó, bạn đã hoàn thành.

Đã trả lời ngày 18 tháng 7 năm 2017 lúc 10:23Jul 18, 2017 at 10:23

Hướng dẫn convert text to html nodejs - chuyển văn bản sang html nodejs

ChazeonchazeonChazeon

5472 Huy hiệu bạc14 Huy hiệu Đồng2 silver badges14 bronze badges

1

Ok cuối cùng, đoạn mã này đã làm việc cho tôi -

template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

Đây là rất nhiều hit và thử nghiệm nhưng cuối cùng nó đã hoạt động.

Đã trả lời ngày 18 tháng 7 năm 2017 lúc 10:48Jul 18, 2017 at 10:48

Hướng dẫn convert text to html nodejs - chuyển văn bản sang html nodejs

JeetjeetJeet

5.3497 Huy hiệu vàng44 Huy hiệu bạc73 Huy hiệu đồng7 gold badges44 silver badges73 bronze badges

3

Giấy phép

Giấy phép MIT

Làm thế nào gửi dữ liệu từ nút JS đến HTML?

/**// w  w  w. j  a va  2s  . co m
 * Converts plain text to HTML
 * @param text the plain text to convert
 * @returns the HTML version of the text
 */
function plain2html(text) {
    text = (text || "");
    return text
        .replace(/&/g, "&")
        .replace(/"<")
        .replace(/>/g, ">")
        .replace(/\t/g, "    ")
        .replace(/ /g, "​ ​")
        .replace(/\r\n|\r|\n/g, "
"
); }

Tạo ứng dụng NodeJS và cài đặt mô -đun:.

  • Bước 1: Chúng tôi có thể tạo một dự án mới với Trình quản lý gói nút bằng cách sử dụng lệnh sau. NPM init ..
  • Bước 2: Cài đặt các phụ thuộc dự án cần thiết, tức là các mô -đun Express và PUG bằng lệnh sau. NPM Cài đặt Express Pug - -Save ..
  • Chúng ta có thể viết HTML trong Node JS không?
  • Bên cạnh các tùy chọn này, bạn luôn có thể tạo các tệp HTML như bình thường và phục vụ chúng qua nút. JS. Để làm điều đó, bạn có thể đọc dữ liệu từ các tệp HTML với hệ thống tệp Node.js và viết nó vào phản hồi.
  • Có thể thể hiện HTML kết xuất không?

HTML-to-text

Hướng dẫn convert text to html nodejs - chuyển văn bản sang html nodejs

Chuyển đổi nâng cao phân tích HTML và trả về văn bản đẹp.

Đặc trưng

  • Thẻ nội tuyến và cấp độ khối.
  • Bảng với colspans và hàng.
  • Liên kết với cả văn bản và href.
  • Gói từ.
  • Hỗ trợ Unicode.
  • Rất nhiều tùy chọn tùy chỉnh.

Thay đổi

Có sẵn ở đây: changelog.md

Phiên bản 6 chứa rất nhiều thay đổi, vì vậy nó đáng để xem xét.

Phiên bản 7 chứa một thay đổi quan trọng cho các định dạng tùy chỉnh.

Phiên bản 8 mang lại cho các bộ chọn hỗ trợ để tăng đáng kể tính linh hoạt nhưng điều đó cũng thay đổi một số điều được giới thiệu trong phiên bản 6. Lựa chọn phần tử cơ sở cũng có những thay đổi quan trọng.

Cài đặt

npm install html-to-text

Cách sử dụng

Chuyển đổi một tài liệu duy nhất:

const { convert } = require('html-to-text');
// There is also an alias to `convert` called `htmlToText`.

const html = '

Hello World

'
; const text = convert(html, { wordwrap: 130 }); console.log(text); // Hello World

Định cấu hình

Title

First line.
Second line.

Footer
6 Một lần để xử lý hàng loạt:

const { compile } = require('html-to-text');

const convert = compile({
  wordwrap: 130
});

const htmls = [
  '

Hello World!

'
, '

こんにちは世界!

'
, '

Привет, мир!

'
]; const texts = htmls.map(convert); console.log(texts.join('\n')); // Hello World! // こんにちは世界! // Привет, мир!

Tùy chọn

Tùy chọn chung

Quyền muaMặc địnhSự mô tả
Title

First line.
Second line.

Footer
7
Mô tả phần nào của tài liệu đầu vào phải được chuyển đổi và trình bày trong văn bản đầu ra và theo thứ tự nào.
Title

First line.
Second line.

Footer
8
Title

First line.
Second line.

Footer
9
Các yếu tố phù hợp với bất kỳ bộ chọn được cung cấp nào sẽ được xử lý và bao gồm trong văn bản đầu ra, với tất cả nội dung bên trong. Trao đổi phần chọn được hỗ trợ bên dưới.
Refer to Supported selectors section below.

Title

First line.
Second line.

Footer

0

Title

First line.
Second line.

Footer

1

Title

First line.
Second line.

Footer

1 - Sắp xếp các phần tử cơ sở theo cùng thứ tự với mảng
Title

First line.
Second line.

Footer
8; ________ 24 - Sắp xếp các phần tử cơ sở theo thứ tự chúng được tìm thấy trong tài liệu đầu vào.

Title

First line.
Second line.

Footer

4 - arrange base elements in the order they are found in the input document.

Title

First line.
Second line.

Footer

5

Title

First line.
Second line.

Footer

6
Chuyển đổi toàn bộ tài liệu nếu không có bộ chọn nào được cung cấp khớp.

Title

First line.
Second line.

Footer

7

Title

First line.
Second line.

Footer

8
Các tùy chọn giải mã văn bản được cung cấp cho

Title

First line.
Second line.

Footer

9. Để biết thêm thông tin, hãy xem mô -đun HE.
text.split('\n').join('\n
\n')
0
text.split('\n').join('\n
\n')
1
Một đối tượng có hàm định dạng tùy chỉnh cho các phần tử cụ thể (xem phần Định dạng ghi đè bên dưới).
text.split('\n').join('\n
\n')
2
Mô tả cách giới hạn văn bản đầu ra trong trường hợp các tài liệu HTML lớn.
text.split('\n').join('\n
\n')
3
text.split('\n').join('\n
\n')
4
Một chuỗi để chèn thay cho nội dung bị bỏ qua.
text.split('\n').join('\n
\n')
5
text.split('\n').join('\n
\n')
6
Ngừng tìm kiếm thêm các yếu tố cơ bản sau khi đạt được số tiền này. Không giới hạn nếu không xác định.
text.split('\n').join('\n
\n')
7
text.split('\n').join('\n
\n')
6
Ngừng tìm kiếm thêm các yếu tố cơ bản sau khi đạt được số tiền này. Không giới hạn nếu không xác định.
text.split('\n').join('\n
\n')
7
text.split('\n').join('\n
\n')
6
Ngừng tìm kiếm thêm các yếu tố cơ bản sau khi đạt được số tiền này. Không giới hạn nếu không xác định.
text.split('\n').join('\n
\n')
7
Số lượng nút trẻ em tối đa của một nút duy nhất được thêm vào đầu ra. Không giới hạn nếu không xác định.
text.split('\n').join('\n
\n')
9
Ngừng tìm kiếm các nút để thêm vào đầu ra dưới độ sâu này trong cây Dom. Không giới hạn nếu không xác định.
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

1
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

2
Nếu chuỗi đầu vào dài hơn giá trị này - nó sẽ bị cắt và một thông báo sẽ được gửi đến
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

3. Ellipsis không được sử dụng trong trường hợp này. Không giới hạn nếu không xác định.
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

4
Mô tả cách quấn các từ dài.
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

5
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

6
Một mảng chứa các ký tự có thể được bọc. Được kiểm tra theo thứ tự, có thể đáp ứng các điểm dừng một lần theo yêu cầu độ dài dòng.
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

5
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

6
Một mảng chứa các ký tự có thể được bọc. Được kiểm tra theo thứ tự, có thể đáp ứng các điểm dừng một lần theo yêu cầu độ dài dòng.Nếu chuỗi đầu vào dài hơn giá trị này - nó sẽ bị cắt và một thông báo sẽ được gửi đến
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

3. Ellipsis không được sử dụng trong trường hợp này. Không giới hạn nếu không xác định.
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

4
Mô tả cách quấn các từ dài.
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

5
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

6
Một mảng chứa các ký tự có thể được bọc. Được kiểm tra theo thứ tự, có thể đáp ứng các điểm dừng một lần theo yêu cầu độ dài dòng.
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

7
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

8
Set to
/**// w  w  w. j  a va  2s  . co m
 * Converts plain text to HTML
 * @param text the plain text to convert
 * @returns the HTML version of the text
 */
function plain2html(text) {
    text = (text || "");
    return text
        .replace(/&/g, "&")
        .replace(/"<")
        .replace(/>/g, ">")
        .replace(/\t/g, "    ")
        .replace(/ /g, "​ ​")
        .replace(/\r\n|\r|\n/g, "
"
); }
9 or
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

8 to disable word-wrapping.

Phá vỡ các từ dài ở giới hạn độ dài dòng trong trường hợp không tìm thấy cơ hội bọc tốt hơn.

Old optionDepr.Rem.Instead use
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

9
8.0 Theo mặc định, bất kỳ dòng mới
/**// w  w  w. j  a va  2s  . co m
 * Converts plain text to HTML
 * @param text the plain text to convert
 * @returns the HTML version of the text
 */
function plain2html(text) {
    text = (text || "");
    return text
        .replace(/&/g, "&")
        .replace(/"<")
        .replace(/>/g, ">")
        .replace(/\t/g, "    ")
        .replace(/ /g, "​ ​")
        .replace(/\r\n|\r|\n/g, "
"
); }
1 nào từ HTML đầu vào đều được thu gọn vào không gian như bất kỳ ký tự nào khác về độ nào khác. Nếu

Title

First line.
Second line.

Footer

6, các dòng mới này sẽ được bảo tồn trong đầu ra. Điều này chỉ hữu ích khi HTML đầu vào mang một số định dạng văn bản đơn giản thay vì các thẻ thích hợp.
/**// w  w  w. j  a va  2s  . co m
 * Converts plain text to HTML
 * @param text the plain text to convert
 * @returns the HTML version of the text
 */
function plain2html(text) {
    text = (text || "");
    return text
        .replace(/&/g, "&")
        .replace(/"<")
        .replace(/>/g, ">")
        .replace(/\t/g, "    ")
        .replace(/ /g, "​ ​")
        .replace(/\r\n|\r|\n/g, "
"
); }
3
6.0 Mô tả cách các yếu tố HTML khác nhau nên được định dạng. Xem phần Lựa chọn bên dưới.
/**// w  w  w. j  a va  2s  . co m
 * Converts plain text to HTML
 * @param text the plain text to convert
 * @returns the HTML version of the text
 */
function plain2html(text) {
    text = (text || "");
    return text
        .replace(/&/g, "&")
        .replace(/"<")
        .replace(/>/g, ">")
        .replace(/\t/g, "    ")
        .replace(/ /g, "​ ​")
        .replace(/\r\n|\r|\n/g, "
"
); }
5
6.0 9.0
/**// w  w  w. j  a va  2s  . co m
 * Converts plain text to HTML
 * @param text the plain text to convert
 * @returns the HTML version of the text
 */
function plain2html(text) {
    text = (text || "");
    return text
        .replace(/&/g, "&")
        .replace(/"<")
        .replace(/>/g, ">")
        .replace(/\t/g, "    ")
        .replace(/ /g, "​ ​")
        .replace(/\r\n|\r|\n/g, "
"
); }
6
Một chuỗi các ký tự được công nhận là khoảng trắng HTML. Giá trị mặc định sử dụng tập hợp các ký tự được xác định trong tiêu chuẩn HTML4. (Nó bao gồm không gian không có chiều rộng so với tiêu chuẩn sống.)6.0 9.0
/**// w  w  w. j  a va  2s  . co m
 * Converts plain text to HTML
 * @param text the plain text to convert
 * @returns the HTML version of the text
 */
function plain2html(text) {
    text = (text || "");
    return text
        .replace(/&/g, "&")
        .replace(/"<")
        .replace(/>/g, ">")
        .replace(/\t/g, "    ")
        .replace(/ /g, "​ ​")
        .replace(/\r\n|\r|\n/g, "
"
); }
7
/**// w  w  w. j  a va  2s  . co m
 * Converts plain text to HTML
 * @param text the plain text to convert
 * @returns the HTML version of the text
 */
function plain2html(text) {
    text = (text || "");
    return text
        .replace(/&/g, "&")
        .replace(/"<")
        .replace(/>/g, ">")
        .replace(/\t/g, "    ")
        .replace(/ /g, "​ ​")
        .replace(/\r\n|\r|\n/g, "
"
); }
8
6.0 9.0 Sau khi có bao nhiêu chars, một lần phá vỡ dòng sẽ theo sau.
Các tùy chọn không dùng nữa hoặc bị loại bỏ6.0 9.0
npm install html-to-text
1
const { convert } = require('html-to-text');
// There is also an alias to `convert` called `htmlToText`.

const html = '

Hello World

'
; const text = convert(html, { wordwrap: 130 }); console.log(text); // Hello World
3
const { convert } = require('html-to-text');
// There is also an alias to `convert` called `htmlToText`.

const html = '

Hello World

'
; const text = convert(html, { wordwrap: 130 }); console.log(text); // Hello World
4
const { convert } = require('html-to-text');
// There is also an alias to `convert` called `htmlToText`.

const html = '

Hello World

'
; const text = convert(html, { wordwrap: 130 }); console.log(text); // Hello World
5
npm install html-to-text
2
6.0 9.0
npm install html-to-text
3
Cách viết định dạng được viết đã thay đổi hoàn toàn. Các định dạng mới phải được thêm vào tùy chọn
text.split('\n').join('\n
\n')
0, những cái cũ không thể được sử dụng lại mà không viết lại. Xem hướng dẫn mới dưới đây.
6.0 9.0
npm install html-to-text
5
npm install html-to-text
6
8.0
npm install html-to-text
7
const { compile } = require('html-to-text');

const convert = compile({
  wordwrap: 130
});

const htmls = [
  '

Hello World!

'
, '

こんにちは世界!

'
, '

Привет, мир!

'
]; const texts = htmls.map(convert); console.log(texts.join('\n')); // Hello World! // こんにちは世界! // Привет, мир!
2
6.0 9.0
const { compile } = require('html-to-text');

const convert = compile({
  wordwrap: 130
});

const htmls = [
  '

Hello World!

'
, '

こんにちは世界!

'
, '

Привет, мир!

'
]; const texts = htmls.map(convert); console.log(texts.join('\n')); // Hello World! // こんにちは世界! // Привет, мир!
4
const { compile } = require('html-to-text');

const convert = compile({
  wordwrap: 130
});

const htmls = [
  '

Hello World!

'
, '

こんにちは世界!

'
, '

Привет, мир!

'
]; const texts = htmls.map(convert); console.log(texts.join('\n')); // Hello World! // こんにちは世界! // Привет, мир!
5
const { convert } = require('html-to-text');
// There is also an alias to `convert` called `htmlToText`.

const html = '

Hello World

'
; const text = convert(html, { wordwrap: 130 }); console.log(text); // Hello World
5
const { compile } = require('html-to-text');

const convert = compile({
  wordwrap: 130
});

const htmls = [
  '

Hello World!

'
, '

こんにちは世界!

'
, '

Привет, мир!

'
]; const texts = htmls.map(convert); console.log(texts.join('\n')); // Hello World! // こんにちは世界! // Привет, мир!
7
8.0
const { compile } = require('html-to-text');

const convert = compile({
  wordwrap: 130
});

const htmls = [
  '

Hello World!

'
, '

こんにちは世界!

'
, '

Привет, мир!

'
]; const texts = htmls.map(convert); console.log(texts.join('\n')); // Hello World! // こんにちは世界! // Привет, мир!
8
const { compile } = require('html-to-text');

const convert = compile({
  wordwrap: 130
});

const htmls = [
  '

Hello World!

'
, '

こんにちは世界!

'
, '

Привет, мир!

'
]; const texts = htmls.map(convert); console.log(texts.join('\n')); // Hello World! // こんにちは世界! // Привет, мир!
9
8.0 Xem phần Lựa chọn bên dưới.
const { convert } = require('html-to-text');

const html = 'PageAction';
const text = convert(html, {
  selectors: [
    { selector: 'a', options: { baseUrl: 'https://example.com' } },
    { selector: 'a.button', format: 'skip' }
  ]
});
console.log(text); // Page [https://example.com/page.html]
0
6.0 9.0
const { convert } = require('html-to-text');

const html = 'PageAction';
const text = convert(html, {
  selectors: [
    { selector: 'a', options: { baseUrl: 'https://example.com' } },
    { selector: 'a.button', format: 'skip' }
  ]
});
console.log(text); // Page [https://example.com/page.html]
1
const { convert } = require('html-to-text');

const html = 'PageAction';
const text = convert(html, {
  selectors: [
    { selector: 'a', options: { baseUrl: 'https://example.com' } },
    { selector: 'a.button', format: 'skip' }
  ]
});
console.log(text); // Page [https://example.com/page.html]
2
6.0 9.0 Các
const { convert } = require('html-to-text');

const html = 'PageAction';
const text = convert(html, {
  selectors: [
    { selector: 'a', options: { baseUrl: 'https://example.com' } },
    { selector: 'a.button', format: 'skip' }
  ]
});
console.log(text); // Page [https://example.com/page.html]
4
const { convert } = require('html-to-text');

const html = 'PageAction';
const text = convert(html, {
  selectors: [
    { selector: 'a', options: { baseUrl: 'https://example.com' } },
    { selector: 'a.button', format: 'skip' }
  ]
});
console.log(text); // Page [https://example.com/page.html]
5
const { convert } = require('html-to-text');

const html = 'PageAction';
const text = convert(html, {
  selectors: [
    { selector: 'a', options: { baseUrl: 'https://example.com' } },
    { selector: 'a.button', format: 'skip' }
  ]
});
console.log(text); // Page [https://example.com/page.html]
6
const { convert } = require('html-to-text');
// There is also an alias to `convert` called `htmlToText`.

const html = '

Hello World

'
; const text = convert(html, { wordwrap: 130 }); console.log(text); // Hello World
5

Những thứ khác không còn bị ảnh hưởng:

  • Phương pháp
    const { convert } = require('html-to-text');
    
    const html = 'PageAction';
    const text = convert(html, {
      selectors: [
        { selector: 'a', options: { baseUrl: 'https://example.com' } },
        { selector: 'a.button', format: 'skip' }
      ]
    });
    console.log(text); // Page [https://example.com/page.html]
    8;
  • Đối số vị trí trong các phương thức
    const { convert } = require('html-to-text');
    
    const html = 'PageAction';
    const text = convert(html, {
      selectors: [
        { selector: 'a', options: { baseUrl: 'https://example.com' } },
        { selector: 'a.button', format: 'skip' }
      ]
    });
    console.log(text); // Page [https://example.com/page.html]
    9 (trong trường hợp bạn đã viết một số định dạng tùy chỉnh cho phiên bản 6.0).

Người chọn

Một số ví dụ:

const { convert } = require('html-to-text');

const html = 'PageAction';
const text = convert(html, {
  selectors: [
    { selector: 'a', options: { baseUrl: 'https://example.com' } },
    { selector: 'a.button', format: 'skip' }
  ]
});
console.log(text); // Page [https://example.com/page.html]

Mảng chọn lọc là xấp xỉ lỏng lẻo của chúng tôi của một bảng kiểu.

  • Bộ chọn độ đặc hiệu cao nhất được sử dụng khi có nhiều trận đấu;
  • Bộ chọn cuối cùng được sử dụng khi có nhiều trận đấu có độ đặc hiệu bằng nhau;
  • Tất cả các mục có cùng giá trị bộ chọn được hợp nhất (đệ quy) ở giai đoạn biên dịch, theo cách như vậy để các thuộc tính được xác định cuối cùng được giữ và thứ tự tương đối của các bộ chọn duy nhất được giữ;
  • Các mục do người dùng xác định được thêm vào sau các mục được xác định trước;
  • Mỗi bộ chọn duy nhất phải có giá trị
    npm install html-to-text
    
    3 được chỉ định (ít nhất một lần);
  • Không giống như trong CSS, các giá trị từ các bộ chọn phù hợp khác nhau không được hợp nhất ở giai đoạn chuyển đổi. Trận đấu tốt nhất được sử dụng thay thế (đó là lần cuối cùng có độ đặc hiệu cao nhất).

Để đạt được hiệu suất tốt nhất khi kiểm tra từng phần tử DOM so với các bộ chọn được cung cấp, chúng được biên dịch thành cây quyết định. Nhưng nó cũng quan trọng như thế nào bạn chọn bộ chọn. Ví dụ:

Title

First line.
Second line.

Footer
01 tốt hơn nhiều so với
Title

First line.
Second line.

Footer
02 - cái trước sẽ chỉ kiểm tra các div cho ID trong khi cái sau phải kiểm tra mọi phần tử trong DOM.

Bộ chọn được hỗ trợ

Title

First line.
Second line.

Footer
6 dựa vào các gói Parseley và Selderee cho hỗ trợ bộ chọn.

Các bộ chọn sau có thể được sử dụng trong bất kỳ kết hợp nào:

  • Title
    
    First line.
    Second line.
    
    Footer
    
    04 - Bộ chọn phổ quát;
  • Title
    
    First line.
    Second line.
    
    Footer
    
    05 - Tên thẻ;
  • Title
    
    First line.
    Second line.
    
    Footer
    
    06 - Tên lớp;
  • Title
    
    First line.
    Second line.
    
    Footer
    
    07 - ID;
  • Title
    
    First line.
    Second line.
    
    Footer
    
    08 - sự hiện diện thuộc tính;
  • Title
    
    First line.
    Second line.
    
    Footer
    
    09 - Giá trị thuộc tính (với bất kỳ toán tử nào và cả trích dẫn và các sửa đổi độ nhạy trường hợp);
  • Title
    
    First line.
    Second line.
    
    Footer
    
    10 và
    Title
    
    First line.
    Second line.
    
    Footer
    
    11 Combinators (các tổ hợp khác không được hỗ trợ).

Bạn có thể khớp

Title

First line.
Second line.

Footer
12 với
Title

First line.
Second line.

Footer
13 chẳng hạn.

Định dạng được xác định trước

Các bộ chọn sau có một định dạng được chỉ định là một phần của cấu hình mặc định. Mọi thứ có thể bị ghi đè, nhưng bạn không phải lặp lại

npm install html-to-text
3 hoặc các tùy chọn mà bạn không muốn ghi đè. (Nhưng hãy nhớ điều này chỉ đúng với cùng một bộ chọn. Không có kết nối giữa các bộ chọn khác nhau.)

Bộ chọnDefault formatGhi chú
Title

First line.
Second line.

Footer
04
Title

First line.
Second line.

Footer
16
Bộ chọn phổ quát.
Title

First line.
Second line.

Footer
17
Title

First line.
Second line.

Footer
18
Title

First line.
Second line.

Footer
19
Title

First line.
Second line.

Footer
20
Title

First line.
Second line.

Footer
21
Title

First line.
Second line.

Footer
20
Title

First line.
Second line.

Footer
21
Title

First line.
Second line.

Footer
21
Title

First line.
Second line.

Footer
23
Title

First line.
Second line.

Footer
25
Title

First line.
Second line.

Footer
26
Title

First line.
Second line.

Footer
20
Title

First line.
Second line.

Footer
21
Title

First line.
Second line.

Footer
20
Title

First line.
Second line.

Footer
21
Title

First line.
Second line.

Footer
20
Title

First line.
Second line.

Footer
21
Title

First line.
Second line.

Footer
23
Title

First line.
Second line.

Footer
21
Title

First line.
Second line.

Footer
23
Title

First line.
Second line.

Footer
25
Title

First line.
Second line.

Footer
23
Title

First line.
Second line.

Footer
25
Title

First line.
Second line.

Footer
23
Title

First line.
Second line.

Footer
25
Title

First line.
Second line.

Footer
23
Title

First line.
Second line.

Footer
25
Title

First line.
Second line.

Footer
23
Title

First line.
Second line.

Footer
25
Title

First line.
Second line.

Footer
20
Title

First line.
Second line.

Footer
21
Title

First line.
Second line.

Footer
23
Title

First line.
Second line.

Footer
25
Title

First line.
Second line.

Footer
26
Title

First line.
Second line.

Footer
05
Title

First line.
Second line.

Footer
20
Title

First line.
Second line.

Footer
21
Title

First line.
Second line.

Footer
20
Title

First line.
Second line.

Footer
21
Title

First line.
Second line.

Footer
23
Title

First line.
Second line.

Footer
25
Title

First line.
Second line.

Footer
26
Title

First line.
Second line.

Footer
05
Title

First line.
Second line.

Footer
05
Title

First line.
Second line.

Footer
29
Title

First line.
Second line.

Footer
29
Title

First line.
Second line.

Footer
31
Title

First line.
Second line.

Footer
33
Title

First line.
Second line.

Footer
34
Title

First line.
Second line.

Footer
37
Title

First line.
Second line.

Footer
37

Title

First line.
Second line.

Footer
39

  • Title
    
    First line.
    Second line.
    
    Footer
    
    41
  • Title
    
    First line.
    Second line.
    
    Footer
    
    43
Title

First line.
Second line.

Footer
45

Title

First line.
Second line.

Footer
47

Title

First line.
Second line.

Footer
48
Title

First line.
Second line.

Footer
49
Applies to
Title

First line.
Second line.

Footer
50
Title

First line.
Second line.

Footer
51
Title

First line.
Second line.

Footer
53
Title

First line.
Second line.

Footer
55
Title

First line.
Second line.

Footer
56
Note that N+1 line breaks are needed to make N empty lines.
Title

First line.
Second line.

Footer
57
Title

First line.
Second line.

Footer
58
Title

First line.
Second line.

Footer
55
Title

First line.
Second line.

Footer
56
Note that N+1 line breaks are needed to make N empty lines.
Title

First line.
Second line.

Footer
57
Title

First line.
Second line.

Footer
58
Title

First line.
Second line.

Footer
59
Title

First line.
Second line.

Footer
61
For example, with
Title

First line.
Second line.

Footer
85 and
Title

First line.
Second line.

Footer
86 the link in the text will be
Title

First line.
Second line.

Footer
87.
Keep in mind that
Title

First line.
Second line.

Footer
78 should not end with a
Title

First line.
Second line.

Footer
84.
Tương đương với
Title

First line.
Second line.

Footer
20. Sử dụng
Title

First line.
Second line.

Footer
64 thay vào đó cho dữ liệu bảng.
Title

First line.
Second line.

Footer
65
Title

First line.
Second line.

Footer
59
Title

First line.
Second line.

Footer
61
Set to
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

8 or
Title

First line.
Second line.

Footer
95 to disable.
Tương đương với
Title

First line.
Second line.

Footer
20. Sử dụng
Title

First line.
Second line.

Footer
64 thay vào đó cho dữ liệu bảng.
Title

First line.
Second line.

Footer
65
Title

First line.
Second line.

Footer
18
Title

First line.
Second line.

Footer
19
Title

First line.
Second line.

Footer
99 => becomes =>

Title

First line.
Second line.

Footer

00.
If this option is set to

Title

First line.
Second line.

Footer

6 and

Title

First line.
Second line.

Footer

02 and

Title

First line.
Second line.

Footer

03 are the same,

Title

First line.
Second line.

Footer

04 will be omitted and only

Title

First line.
Second line.

Footer

03 will be present.
npm install html-to-text
7
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

8
Title

First line.
Second line.

Footer
18
Bỏ qua tất cả các liên kết. Chỉ xử lý văn bản nội bộ của các thẻ neo.
const { convert } = require('html-to-text');
// There is also an alias to `convert` called `htmlToText`.

const html = '

Hello World

'
; const text = convert(html, { wordwrap: 130 }); console.log(text); // Hello World
6

Title

First line.
Second line.

Footer

6
Title

First line.
Second line.

Footer
18
Bỏ qua tất cả các liên kết. Chỉ xử lý văn bản nội bộ của các thẻ neo.
const { convert } = require('html-to-text');
// There is also an alias to `convert` called `htmlToText`.

const html = '

Hello World

'
; const text = convert(html, { wordwrap: 130 }); console.log(text); // Hello World
6

Title

First line.
Second line.

Footer

6
Bỏ qua các liên kết neo (trong đó

Title

First line.
Second line.

Footer

12).

Title

First line.
Second line.

Footer

13

Title

First line.
Second line.

Footer

14

Title

First line.
Second line.

Footer

6
Bỏ qua các liên kết neo (trong đó

Title

First line.
Second line.

Footer

12).

Title

First line.
Second line.

Footer

13
Set this to
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

8 to leave headings as they are.

Title

First line.
Second line.

Footer

14
Title

First line.
Second line.

Footer
66
Tiền tố chuỗi cho mỗi mục danh sách.

Title

First line.
Second line.

Footer

16
Title

First line.
Second line.

Footer
34

Title

First line.
Second line.

Footer

6
Bỏ qua các liên kết neo (trong đó

Title

First line.
Second line.

Footer

12).

Title

First line.
Second line.

Footer

13
While empty lines should be preserved in HTML, space-saving behavior is chosen as default for convenience.

Title

First line.
Second line.

Footer

14

Title

First line.
Second line.

Footer

6
Bỏ qua các liên kết neo (trong đó

Title

First line.
Second line.

Footer

12).

Title

First line.
Second line.

Footer

13
Set this to
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

8 to leave heading cells as they are.

Title

First line.
Second line.

Footer

14
Title

First line.
Second line.

Footer
66
Bỏ qua các liên kết neo (trong đó

Title

First line.
Second line.

Footer

12).

Title

First line.
Second line.

Footer

13
Set this to
text.split('\n').join('\n
\n')
6 in order to fall back to
/**// w  w  w. j  a va  2s  . co m
 * Converts plain text to HTML
 * @param text the plain text to convert
 * @returns the HTML version of the text
 */
function plain2html(text) {
    text = (text || "");
    return text
        .replace(/&/g, "&")
        .replace(/"<")
        .replace(/>/g, ">")
        .replace(/\t/g, "    ")
        .replace(/ /g, "​ ​")
        .replace(/\r\n|\r|\n/g, "
"
); }
7 limit.

Title

First line.
Second line.

Footer

14
Title

First line.
Second line.

Footer
66
Bỏ qua các liên kết neo (trong đó

Title

First line.
Second line.

Footer

12).

Title

First line.
Second line.

Footer

13

Title

First line.
Second line.

Footer

14
Title

First line.
Second line.

Footer
66
Bỏ qua các liên kết neo (trong đó

Title

First line.
Second line.

Footer

12).

Title

First line.
Second line.

Footer

13

Title

First line.
Second line.

Footer

14
Title

First line.
Second line.

Footer
66
Applies toDepr.Rem.Tiền tố chuỗi cho mỗi mục danh sách.

Title

First line.
Second line.

Footer

16
Title

First line.
Second line.

Footer
18
8.1
Title

First line.
Second line.

Footer
34

Title

First line.
Second line.

Footer

22

text.split('\n').join('\n
\n')
6

Title

First line.
Second line.

Footer
48

  • Chiều dài của dòng. Nếu không xác định thì giá trị
    /**// w  w  w. j  a va  2s  . co m
     * Converts plain text to HTML
     * @param text the plain text to convert
     * @returns the HTML version of the text
     */
    function plain2html(text) {
        text = (text || "");
        return text
            .replace(/&/g, "&")
            .replace(/"<")
            .replace(/>/g, ">")
            .replace(/\t/g, "    ")
            .replace(/ /g, "​ ​")
            .replace(/\r\n|\r|\n/g, "
    "
    ); }
    7 được sử dụng. Rơi trở lại 40 nếu điều đó cũng bị vô hiệu hóa.
  • Title

    First line.
    Second line.

    Footer

    26
  • Title
    
    First line.
    Second line.
    
    Footer
    
    23
  • Cắt các dòng trống từ blockquote. Trong khi các dòng trống nên được bảo tồn trong HTML, hành vi tiết kiệm không gian được chọn làm mặc định để thuận tiện.

Title

First line.
Second line.

Footer

29

Title

First line.
Second line.

Footer
0

Title

First line.
Second line.

Footer
64

Theo mặc định, các ô tiêu đề (

Title

First line.
Second line.

Footer

32) được sử dụng trên đường. Điều này thành
template = template.replace(/\n/gi, "


") template = template.replace(/<\/p>/gi, "


")

8 để để lại các tế bào tiêu đề như chúng.

Title

First line.
Second line.

Footer

34

Nội dung ô bảng dữ liệu sẽ được bọc để phù hợp với chiều rộng này thay vì giới hạn toàn cầu ____57. Điều này đến

text.split('\n').join('\n
\n')
6 để quay trở lại giới hạn
/**// w  w  w. j  a va  2s  . co m
 * Converts plain text to HTML
 * @param text the plain text to convert
 * @returns the HTML version of the text
 */
function plain2html(text) {
    text = (text || "");
    return text
        .replace(/&/g, "&")
        .replace(/"<")
        .replace(/>/g, ">")
        .replace(/\t/g, "    ")
        .replace(/ /g, "​ ​")
        .replace(/\r\n|\r|\n/g, "
"
); }
7.

Title

First line.
Second line.

Footer

40

Title

First line.
Second line.

Footer
1

Title

First line.
Second line.

Footer
74

Title

First line.
Second line.

Footer
2

Số lượng khoảng trống giữa các cột bảng dữ liệu.

  • Title

    First line.
    Second line.

    Footer

    44
  • Số lượng dòng trống giữa các hàng bảng dữ liệu.

Tùy chọn định dạng không dùng nữa

  • Tùy chọn cũ
  • Thay vào đó sử dụng
  • const { convert } = require('html-to-text');
    // There is also an alias to `convert` called `htmlToText`.
    
    const html = '

    Hello World

    '
    ; const text = convert(html, { wordwrap: 130 }); console.log(text); // Hello World
    8

Ghi đè định dạng

Làm thế nào gửi dữ liệu từ nút JS đến HTML?

Tạo ứng dụng NodeJS và cài đặt mô -đun:..
Bước 1: Chúng tôi có thể tạo một dự án mới với Trình quản lý gói nút bằng cách sử dụng lệnh sau.NPM init ..
Bước 2: Cài đặt các phụ thuộc dự án cần thiết, tức là các mô -đun Express và PUG bằng lệnh sau.NPM Cài đặt Express Pug - -Save ..

Chúng ta có thể viết HTML trong Node JS không?

Bên cạnh các tùy chọn này, bạn luôn có thể tạo các tệp HTML như bình thường và phục vụ chúng qua nút.JS.Để làm điều đó, bạn có thể đọc dữ liệu từ các tệp HTML với hệ thống tệp Node.js và viết nó vào phản hồi.you can read data from HTML files with Node. js File System and write it into response.

Có thể thể hiện HTML kết xuất không?

Phương thức Res.SendFile () của mô -đun Express.js được sử dụng để hiển thị một tệp HTML cụ thể có trong máy cục bộ. sendFile() method of the express. js module is used to render a particular HTML file that is present in the local machine.

Node JS Fireeship là gì?

Node.js là môi trường thời gian chạy và thực hiện để xây dựng các ứng dụng JavaScript trên máy chủ.a runtime and execution environment for building JavaScript apps on the server.