npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-to-blocks

v1.0.6

Published

Use this package when you have a need to accept html from a CMS and parse/transform it into components.

Downloads

14

Readme

html-to-blocks

Stackblitz

https://stackblitz.com/edit/angular-html-to-blocks

Summary

Use this package when you have a need to accept html from a CMS and parse/transform it into components. For example, if you need to extract videos, images, photo galleries, or other functionality from html a content manager enters in a CMS.

You provide the input html and a list of blocks to extract and how to extract them (selectors/factories). The html is parsed into a DOM and transformed into a list of blocks based on the selectors/factories you provide. Elements not matched by the selector are, by default, transformed into html blocks. Elements matched by the selectors are extracted from wherever deep in the DOM they may be to be children of the root element. So, for example, if you have a video deep within nested html elements, the surrounding content will be split into 3 blocks: 1 html block, 1 video block then 1 more html block. This process of splitting a deeply nested html to lift the video to the top level requires recursive cloning of the video's parent elements, so you will end up with more container elements in your html blocks than the input html, but you should not have duplicated content, as siblings will not be duplicated in that process.

This library does not implement the way to format/display these blocks. The scope of this project is to accept html and parse it into a list of blocks. However, once you have your list of blocks, formatting/displaying is trivial (see example).

Usage (Angular)

import { Component, Input } from "@angular/core";
import { DomSanitizer } from "@angular/platform-browser";
import { Block, BlockParser } from "html-to-blocks";

@Component({
  selector: "app-blocks",
  template: `
    <ng-container *ngFor="let block of blocks">
      <div *ngIf="block.html" [innerHTML]="block.html"></div>
      <iframe *ngIf="block.youtubeUrl" [src]="block.youtubeUrl"></iframe>
    </ng-container>
  `,
})
export class BlocksComponent {
  blocks: Block[] = [];
  blockParser: BlockParser = new BlockParser();

  constructor(private domSanitizer: DomSanitizer) {}

  @Input()
  set html(html: string) {
    this.blocks = this.blockParser.parseHtmlToBlocks(html, [
      {
        selector: "iframe[src*='youtube.com'],iframe[src*='youtu.be']",
        factory: (iframe: HTMLIFrameElement) => {
          const youtubeUrl = this.domSanitizer.bypassSecurityTrustResourceUrl(
            "https://www.youtube.com/embed/" + parseYoutubeIdFromUrl(iframe.src),
          );
          return { youtubeUrl };
        },
      },
    ]);
  }
}