SmartBlocks icon indicating copy to clipboard operation
SmartBlocks copied to clipboard

Find and Replace SmartBlock. Replace a string in a block with another string.

Open GitMurf opened this issue 4 years ago • 2 comments

✂️ Copy of your 42SmartBlock from Roam

  • #42SmartBlock Find-Replace (input)
    
    • <%SET:find1,<%INPUT:Character(s) to find:%%find%>%><%NOBLOCKOUTPUT%>
      
    • <%SET:rep1,<%INPUT:Replace "<%GET:find1%>" with?%%replace%>%><%NOBLOCKOUTPUT%>
      
    • <%JAVASCRIPT:
      ```javascript
      
      var f1 = roam42.smartBlocks.activeWorkflow.vars["find1"];
      var r1 = roam42.smartBlocks.activeWorkflow.vars["rep1"];
      
      var curBlock = roam42.smartBlocks.activeWorkflow.startingBlockContents;
      var newBlock = curBlock.split(f1).join(r1);
      document.activeElement.value = '';
      return newBlock.trim();
      ```
      %>
      
  • #42SmartBlock Find-Replace (z_hardcoded)
    
    • <%SET:find1,FINDME1%><%NOBLOCKOUTPUT%>
      
    • <%SET:rep1,REPLACEME1%><%NOBLOCKOUTPUT%>
      
    • <%SET:find2,FINDME2%><%NOBLOCKOUTPUT%>
      
    • <%SET:rep2,REPLACEME2%><%NOBLOCKOUTPUT%>
      
    • <%JAVASCRIPT:
      ```javascript
      
      var f1 = roam42.smartBlocks.activeWorkflow.vars["find1"];
      var r1 = roam42.smartBlocks.activeWorkflow.vars["rep1"];
      var f2 = roam42.smartBlocks.activeWorkflow.vars["find2"];
      var r2 = roam42.smartBlocks.activeWorkflow.vars["rep2"];
      
      //var curBlock = roam42.smartBlocks.activeWorkflow.startingBlockContents;
      var curBlock = document.activeElement.value;
      var newBlock = curBlock.split(f1).join(r1).split(f2).join(r2);
      document.activeElement.value = '';
      return newBlock.trim();
      ```
      %>
      

📋 Describe the SmartBlock

Just posted a new SmartBlock for Find/Replace in your current block! First popup input asks what you want to "find" and the second input asks what you want to "replace" it with.

I also created a "hardcoded" version so you can set it to find/replace something and not require a popup input each time. Good if you have several of the same things you want to replace on several blocks and quickly want to go through the blocks and find/replace with the same things.

✅ Describe any prerequisites or dependencies that are required for this SmartBlock

roam42

📷 Screenshot of your 42SmartBlock workflow/template from Roam

💡 Additional Info


[label_example]

GitMurf avatar Dec 14 '20 11:12 GitMurf

This can be used to fix Wiki links too based on this issue #117

GitMurf avatar Dec 14 '20 12:12 GitMurf

Thanks for this! Lack of search/replace function in Roam is a huge problem you are helping to address.

Can the smartblock be modified to include all child blocks, so you don't have to run it in each block manually? That would make it much more useful.

Later enhancements could include allowing skip/confirm replace for each instance of the string.

cobblepot1 avatar Jan 07 '21 07:01 cobblepot1