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

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

const { convert } = require['html-to-text'];

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

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

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

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

const html = 'PageAction';
const text = convert[html, {
  selectors: [
    { selector: 'a', options: { baseUrl: '//example.com' } },
    { selector: 'a.button', format: 'skip' }
  ]
}];
console.log[text]; // Page [//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: '//example.com' } },
        { selector: 'a.button', format: 'skip' }
      ]
    }];
    console.log[text]; // Page [//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: '//example.com' } },
        { selector: 'a.button', format: 'skip' }
      ]
    }];
    console.log[text]; // Page [//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: '//example.com' } },
    { selector: 'a.button', format: 'skip' }
  ]
}];
console.log[text]; // Page [//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
49Applies 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[//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[//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[//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[//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[//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
66Applies 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

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

Title

First line.
Second line.

Footer

19,

Title

First line.
Second line.

Footer

19, v.v.] được sử dụng. Điều này đến
template = template.replace[/\n/gi, "


"] template = template.replace[//gi, "


"]

8 để để lại các tiêu đề như hiện tại.

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[//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[//gi, "


"]

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

Title

First line.
Second line.

Footer

34

Title

First line.
Second line.

Footer

35

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[//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

43

  • 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

Title

First line.
Second line.

Footer

48

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.

Bài Viết Liên Quan

Toplist mới

Bài mới nhất

Chủ Đề