Template Options

In this section, you will learn about the structure of templates for parsing.

To create a template for parsing using the news-parser, follow these instructions based on the provided reference information:

  1. Begin by creating a template with the following structure:

{
  "template": {
    "className": "[className]",
    "tagName": "[tagName]",
    "searchTemplate": "[searchTemplate]",
    "children": [
      {
        "tagName": "[tagName]",
        "searchTemplate": "[searchTemplate]",
        "position": "all"
      },
      // Add more child elements as needed
    ]
  },
  "extraOptions": {
    "addSrcSetAndSizes": false,
    "groupImagesRow": false,
    "addFeaturedMedia": false,
    "addSource": false
  },
  "postOptions": {},
  "userID": "[userID]"
}

Replace [className], [tagName], [searchTemplate], and [userID] with the actual values for your template.

  1. In the template, the "className" property represents the HTML class name of the tag that contains the post content. The "tagName" property represents the tag name of the tag that contains the post content. The "searchTemplate" property is the search pattern used to retrieve the node from the DOM, for example, "div.pre-center". Syntax of "searchTemplate" described here: https://simplehtmldom.sourceforge.io/docs/1.9/manual/finding-html-elements/

  2. The "children" property is an array of child node elements that contain elements of the post data. Each child element has the same structure as the parent node, with "tagName", "searchTemplate", and "position" properties. "position" is currently set to "all", indicating that all matching nodes should be retrieved. The child elements are specified in a single array without considering their nesting level.

  3. The "extraOptions" property allows you to enable or disable additional options for the parsed data. Set the "addSrcSetAndSizes" property to true if you want to add the sizes attribute with image sizes breakpoints. Set "groupImagesRow" to true to group images in Gutenberg blocks by two and arrange them in a row. Set "addFeaturedMedia" to true if you want to include a featured media element in the post. Set "addSource" to true if you want to add a link to the source page at the end of the post.

  4. The "postOptions" property can be used to specify additional options for the parsed post.

  5. Finally, replace [userID] with the relevant user ID associated with the template.

That's it! You now have a template structure for parsing using the news-parser. Customize the template according to your specific needs and use it with the news-parser to parse data from the provided URL or other URLs that match the template structure.

Last updated