-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why is attrs not an object? #142
Comments
Because ordering makes sence for testing, and no needs to care about hacks like "proto" name and so on. |
@puzrin How can I reliably access / select the href value of a node? |
Am I doing this right? var _ = require('lodash')
var fs = require('fs')
var MarkdownIt = require('markdown-it')
var md = new MarkdownIt()
var src = fs.readFileSync('./check-asAuthored.md', 'utf8')
var nodes = md.parse(src)
_.each(nodes, function (node) {
if (node.type === 'inline') {
_.each(node.children, function (child) {
if (child.attrs) {
var hrefIndex = child.attrIndex('href')
var hrefValue = child.attrs[hrefIndex][1]
console.log(hrefValue)
}
})
}
}) |
@puzrin looking for a better way of doing this. :) function anchor (node) {
var anchor = {}
anchor.text = undefined
anchor.href = undefined
if (node.type === 'inline' && node.content) {
var pattern = /\[(.+)?\]\((.+)?\)/
var pieces = node.content.match(pattern)
if (!pieces) return anchor
anchor.text = pieces[1]
anchor.href = pieces[2]
}
return anchor
}
nodes = _.map(nodes, function (node) {
node.anchor = anchor(node)
console.log(node.anchor)
return node
}) |
@puzrin I now undertand type inline & my regex breaks here
|
chrisjsewell
added a commit
to executablebooks/markdown-it-py
that referenced
this issue
Mar 17, 2021
Instead of storing `attrs` as `[["key1", "value1"], ["key2", "value2"]]`, use `{"key1": "value1", "key2": "value2"}`. Upstream the list format is only used to guarantee order: markdown-it/markdown-it#142, but in Python 3.7+ dictionary order is now guaranteed by the specification (in Python 3.6 it is also preserved as an implementation detail). This change improves typing and performance. One should anyhow generally use the `attrGet`, `attrSet`, `attrPush` and `attrJoin` methods to manipulate `Token.attrs`, which all have an identical signature to those upstream. To minimize how breaking this change is, auto-conversion is done on `Token` initiation, i.e. you can still use `Token("type", "tag", 0, attrs=[["key", "value"]])`, and also `Token.as_dict(as_upstream=True)` converts the dict back to `null`/`list`, o that they can still be directly compared to those produced in the `debug` tab of https://markdown-it.github.io/. The `meta_serializer` option has also been added to `Token.as_dict`, which now ensures that this method is always able to produce valid JSON.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm curious as to why this isn't a key / value pair?
The text was updated successfully, but these errors were encountered: