Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Plain-text "summary" RDMD export method. #37

Copy link
Copy link
@rafegoldberg

Description

@rafegoldberg
Issue body actions

Let's add a previewText export method to the RDMD engine to loop through the AST and remove any non-paragraph nodes. Then we can transform that to plain-text, and do the truncation in the top-level method. So it'd look something like:

import remarkToPlainText from 'remark-plain-text';
import visit from 'unist-util-flatmap';

const pTagFilter = node => node.type==='paragraph' ? node : null);
//^Our custom p tag filter "plugin"; this is all
// pseudo code, but it works something like this!

export function previewText(text, truncateAt, opts = {}) {
  if (!text) return null;
  [text, opts] = setup(text, opts);

  text = processor(opts)
    .use(tree => visit(tree, pTagFilter))
    .use(remarkToPlainText)
    .use(rehypeStringify)
    .processSync(text)
    .contents;

  return text.slice(0, truncateAt).trim() + '...';
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.