Skip to content
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

Closed
reggi opened this issue Aug 20, 2015 · 6 comments
Closed

Why is attrs not an object? #142

reggi opened this issue Aug 20, 2015 · 6 comments

Comments

@reggi
Copy link
Contributor

reggi commented Aug 20, 2015

I'm curious as to why this isn't a key / value pair?

"attrs": [
  [
    "href",
    "#fragment"
  ]
],
@puzrin
Copy link
Member

puzrin commented Aug 20, 2015

Because ordering makes sence for testing, and no needs to care about hacks like "proto" name and so on.

@reggi
Copy link
Contributor Author

reggi commented Aug 20, 2015

@puzrin How can I reliably access / select the href value of a node?

@puzrin
Copy link
Member

puzrin commented Aug 20, 2015

@reggi
Copy link
Contributor Author

reggi commented Aug 20, 2015

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)
      }
    })
  }
})

@reggi
Copy link
Contributor Author

reggi commented Aug 20, 2015

@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
})

@reggi
Copy link
Contributor Author

reggi commented Aug 20, 2015

@puzrin I now undertand type inline & my regex breaks here

 content: '[](#preventEval)\n[./winning.js](#fileEval)',

@puzrin puzrin closed this as completed Aug 21, 2015
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants