How to get the determined attributes from a mark

Hi,

How can I get an attribute that I have set when creating a mark? From the example-setup, I created a ‘link’ with toggleMark. A nice dialog sets the ‘title’ and the ‘href’ attribute… That all works fine, but… how can I get (and change) an already created ‘link’?

I used ‘state.doc’ to get a selection, and I know that ‘rangeHasMark’ can check if within the selection, the mark exists… but how can I get the ‘already determined attributes’ from an ‘already created link’?

Find the mark and read its attributes? The nodesBetween method is probably helpful when scanning a given range for a mark.

I realize this is an old post however my code may be of help to someone with this issue. Note that I am very new to Prosemirror, so there may well be better ways to accomplish this.

/** Get mark at `pos` for `markType`
 *
 *  @param {Object} view - EditorView
 *  @param {Number} pos - position. ex. selection.from
 *  @param {Object} markType - the MarkType whose Mark you want. ex. view.state.schema.marks.link
 *
 *  @return { start, link, mark } if mark present, else undefined
 *         ex: { $pos:  ResolvedPos for pos param
 *               start: { index, node, offset } for child node after $pos.parent
 *               mark: { attrs: { href: "http://foo.com", title: null }
 *                       type: MarkType
 *                     }
 *             }
 */
function getMarkAtPos( view, pos, markType ){
const doc = view.state.tr.doc,
      $pos = doc.resolve( pos ),
      start = $pos.parent.childAfter( $pos.parentOffset )

if ( start.node ){   // node is a TextNode in our use case.
    const mark = start.node.marks.find( ( mark ) => mark.type === markType )
    if ( mark ){
        // mlog.note( 'getMarkAtPos() mark:', mark )
        return { $pos, start, mark }
    }
}
}