danielo515 / obsidian-modal-form

Define forms for filling data that you will be able to open from anywhere you can run JS
https://danielorodriguez.com/obsidian-modal-form/
MIT License
183 stars 14 forks source link

[BUG] asFrontmatterString() outputs Number types as Strings #170

Open ChaseTC opened 7 months ago

ChaseTC commented 7 months ago

Describe the bug When using asFrontmatterString() Number type fields are outputted as Strings

To Reproduce Steps to reproduce the bug:

  1. Create a templater template:
    ---
    <%*
    const modalForm = app.plugins.plugins.modalforms.api;
    const result = await modalForm.openForm('example-form');
    tR += result.asFrontmatterString();
    -%>
    ---
  2. Insert the template into a new file
  3. Fill out the form
  4. The age field which is a number type is outputted as a String:
    ---
    Name: Foo
    age: "123"
    is_family: false
    multi_example: []
    multi_example_2: []
    multi_select_dataview: []
    best_fried: ""
    favorite_meal: pizza
    Tags: []
    ---

Expected behavior

Number type fields should be outputted as numbers i.e. age: 123

lextatic commented 3 months ago

I've implemented a workaround for this issue by editing the frontmatterString with a regex replace before adding it to the tR.

In the example regex bellow I'm using the variable age from the example form you showed so it looks for the pattern age: "<somedigits>" and then replaces it with age: <somedigits>. Just edit so it uses the name of your form variable. Simple and effective.


<%*
const modalForm = app.plugins.plugins.modalforms.api;
const result = await modalForm.openForm('example-form');

let frontmatterString = result.asFrontmatterString().replace(/age:\s\"([0-9]*)\"/g, 'age: $1');

tR += frontmatterString;
-%>