Skip to content

documentUrlSubstitutions uses substring replacement, causing conflicts with overlapping keys #364

@adbutterfield

Description

@adbutterfield

When using documentUrlSubstitutions with a custom parser, the CLI applies substring replacement to template file content. This causes incorrect URL substitutions when one key is a prefix of another.

Problem

The substitution logic at

figmaNode = figmaNode.replace(from, to)
uses
String.replace():

Object.entries(config.documentUrlSubstitutions).forEach(([from, to]) => {                                                                                   
  figmaNode = figmaNode.replace(from, to)                                                                                                                   
})                                                                                                                                                          

This performs substring matching rather than exact matching. When two keys share a common prefix, the shorter key can partially match within the longer
key's value, corrupting the URL.

Reproduction

Config:

{                                                                                                                                                           
  "documentUrlSubstitutions": {                                                                                                                             
    "SearchInput": "https://figma.com/file/abc/?node-id=4307-49807",                                                                                
    "SearchInputMenu": "https://figma.com/file/abc/?node-id=15100-76317"                                                                            
  }                                                                                                                                                         
}                                                                                                                                                           

Template file:
// url=SearchInputMenu

Expected: URL resolves to https://figma.com/file/abc/?node-id=15100-76317

Actual: The CLI first applies SearchInput substitution (substring match), resulting in:
https://figma.com/file/abc/?node-id=4307-49807Menu

This produces the error:
Invalid figma node URL: the provided node-id "4307-49807Menu" is invalid
Environment

  • @figma/code-connect version: 1.4.1
  • Parser: custom
  • Platform: macOS

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions