简体   繁体   English

如何解析rxjs中可观察对象数组中的html元素

[英]How do I parse html elements from an observable array of objects in rxjs

In angular 8 I am parsing a wordpress rss feed and using one of its properties, 'content' to build a news scroller. 在角度8中,我正在解析wordpress rss提要,并使用其属性之一“内容”来构建新闻滚动器。 The rss feed is processed into a javascript object using rss-parser from node.js. 使用来自node.js的rss-parser将rss feed处理为javascript对象。

I need to parse out a http link, an image and a few chars of text from between paragraph (p) elements'. 我需要从(p)元素之间解析出一个http链接,一个图像和一些字符。 My problem is that the data I need is contained within the 'content' property and I don't know the encoding or how to parse out the link, image and text and place them into variables I can use within the observable. 我的问题是我需要的数据包含在'content'属性中,我不知道编码或如何解析链接,图像和文本并将它们放入可观察范围内的变量中。

Using Angular and rxjs I am able to derive an array of objects that includes each article and the property I need. 使用Angular和rxjs,我能够派生一个包含每个文章和所需属性的对象数组。 const http$ = this.api.rssSource$(); which is from the angular api.service.ts that gets the feed and returns an observable. 这是从有角度的api.service.ts获取提要并返回一个observable。 Then I map it down to the array of objects using this code: 然后,使用以下代码将其映射到对象数组:

this.newsItems$ = http$ .pipe( map(res => Object.values(res['items']))); and I get this array of 20 items 我得到这20个项目的数组

(20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

each object within the array above looks like this: 上面数组中的每个对象如下所示:

{content: "<a href="https://example.com/"><img width="300" height="200" src="https://example.com/some-image-300x200.jpeg" alt="blah blah blah" /></a><p>A lot of text about something and then something else</p><br /><p>jabber jabber and more jabber</p>↵<p><a href="https://example.com/example.html/" rel="nofollow">...Read More About Blah And Jabber</a></p>↵}

using <div [innerHTML]="item.content"></div> in the angular template I can render html with an image and a lot of text. 在有角度的模板中使用<div [innerHTML]="item.content"></div> ,我可以使用图像和大量文本来呈现html。 However, it is not in the format I want and needs to be shortened and re-arranged. 但是,它不是我想要的格式,需要缩短和重新排列。 I only need the complete 'a href="https://xxx..." ', 'img src="http://xxx..." ' and a single 'p xxxx /p'. 我只需要完整的'a href =“ https:// xxx ...”','img src =“ http:// xxx ...”'和单个'p xxxx / p'。

How can I access the object so that I can then further parse it to populate variables for newsLink, newsImg, shortDes? 如何访问该对象,以便可以进一步对其进行解析以填充newsLink,newsImg,shortDes的变量?

If what you wish to do is manipulate each Object in the emitted array, you can add an array map call inside the RxJS map call: 如果你希望做的是操纵发射阵列中的每个对象,你可以添加一个阵列map的RxJS内部呼叫map电话:

this.newsItems$ = http$.pipe(
  map(res => Object.values(res['items']).map(item => {
    // do item modification here
  }))
);

This will return the modified array. 这将返回修改后的数组。 Alternatively, you can split the array and have it emitted as individual values, and then RxJS map them to modify them: 另外,您可以拆分数组并将其作为单个值发出,然后RxJS map它们以对其进行修改:

this.newsItems$ = http$.pipe(
  switchMap(items => from(items)),
  map(item => // manipulate individual items here)
);

As for the actual parsing itself, that can be achieved using regular expressions and the match() function: 至于实际的解析本身,可以使用正则表达式和match()函数来实现:

arrayOfAnchorTags = item.content.match(/<\s*a[^>]*>(.*?)<\/\s*\s*a>/g);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM