rehypejs / rehype-remark Goto Github PK
View Code? Open in Web Editor NEWplugin to transform from HTML (rehype) to Markdown (remark)
Home Page: https://unifiedjs.com
License: MIT License
plugin to transform from HTML (rehype) to Markdown (remark)
Home Page: https://unifiedjs.com
License: MIT License
Can I sponsor some typescript types for this project?
Util to search for a hast element, ideally a CSS-like selector
I would like to try convert only the content of a page, starting at <main>
, <article>
or [role="main"]
and ignore the rest
I have implemented a very basic search function (kindof getElementsByTagName), but wonder if
A) this is the right approach
B) if there is already something for HAST (in rehype, unfidied world)
const unified = require('unified')
const createStream = require('unified-stream')
const parse = require('rehype-parse')
const rehype2remark = require('rehype-remark')
const stringify = require('remark-stringify')
// note:
const all = require('hast-util-to-mdast/lib/all')
const wrap = require('hast-util-to-mdast/lib/util/wrap')
const processor = unified()
.use(parse)
.use(rehype2remark, { handlers: { body }}) // changes the body
.use(stringify)
// try to use main or article, else fallback to body
function body(h, node) {
return wrap(all(h, search(node, 'main, article') || node))
}
process.stdin.pipe(createStream(processor)).pipe(process.stdout)
// returns first node by tagName
function search(node, tagName) {
const tagNames = tagName.split(',').map(s => s.trim())
const que = node.children
for (const node of que) {
if (node.type === 'element' && tagNames.includes(node.tagName)) {
return node
}
if (node.children && node.children.length) {
que.push(...node.children)
}
}
return null
}
latest
No response
Convert following html to remark
<strong>
Hello Gamer
</strong>
it will be failed to be strong in markdown.
Hello Gamer
**Hello Gamer **
Node v16
yarn 1
Windows
No response
9.1.1
No response
You can see what it is erroring out here: https://cdn.skypack.dev/[email protected] and the error message from skypack here: https://cdn.skypack.dev/error/build:[email protected]
This is also happening when trying to use the package locally
We should be able to import the library without any issues
The lib directory doesn't look to be built so when we try and import the package it's looking for a file that doesn't exist and errors.
Node v16
npm v7
macOS
No response
9.1.0
https://www.npmjs.com/package/rehype-remark
https://www.npmjs.com/package/rehype-remark/v/9.1.0
release 9.1.0 to npm
not released
No response
No response
No response
No response
Retain Props on html nodes
OR
don't convert elements with props
I have been building a markdown editor, for use creating an e-learning platform.
I've utilised Remark and Rehype to create and sanitise both markdown and html within the same document, for flexibility purposes. As an example, I am able to add embedded video players, more customisable tables, safer links etc, but only when required - generally we use markdown for simplicity.
This is all working as expected, with one exception: additional props to html elements -> they are converted back to standard markdown elements.
For example, if I added a open="_blank"
and rel="noreferrer"
properties to an A tag, this would be stripped when converted to remark using rehype-remark.
Code is as follows:
import remark from 'remark'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeRaw from 'rehype-raw'
import rehypeSanitize from 'rehype-sanitize'
import rehypeRemark from 'rehype-remark'
const markdownTagNames = ['a', 'p', 'h1']
const markdown = `
# header
<a href="https://github.com/rehypejs/rehype-remark" open="_blank" rel="noreferrer">rehype-remark link (new tab)</a>
`
remark()
.use(remarkParse)
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeSanitize, { tagNames: markdownTagNames })
.use(rehypeRemark)
.processSync(markdown).contents;
This outputs:
# header
[rehype-remark link (new tab)](https://github.com/rehypejs/rehype-remark)
Rehype documents containing markdown convertible-nodes should only convert if no properties exist.
The main reason I was trying to convert the rehype back to remark was to save the sanitised input, rather than the markdown with tags in the .md file, and sanitize every time they're presented.
In the mean-time, there are two workarounds:
saving the unsanitised .md files
This could contain <script> tags for example, but sanitizing them upon presentation.
The main risk of this approach would be forgetting to sanitize the document.
Injecting open/noreferrer to all a tags on the presentation layer
A bit carte blanche, but resolves the pen-test issue.
Creating a separate node for custom conversion
More a theory, but possibly the most abstract way of doing it. This would result in having non common markup options though.
rehype-remark 9.1.2, remark-stringify 10.0.2, rehype-parse 8.0.4
No response
import rehypeParse from 'rehype-parse';
import rehypeRemark from 'rehype-remark';
import remarkStringify from 'remark-stringify';
main();
async function main() {
const file = unified()
.use(rehypeParse)
.use(rehypeRemark)
.use(remarkStringify)
.processSync('<h1>Q&A</h1>');
console.log(String(file));
}
The output should be # Q&A
The output is # Q\&A
Looking at the generated string it looks like #Q\\&A
instead of #Q\&A
Node v16
npm 8
macOS
No response
10
No response
<strong>Test: </strong>The test
is transformed to **Test: **The test
This was originally observed in the wild while copying text from nytimes.com
Valid markdown output.
Invalid markdown output.
No response
No response
No response
No response
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.