LongTengDao / j-toml

A Node.js implementation of TOML written by LongTengDao. Belong to "Plan J"./龙腾道为汤小明语写的 Node.js 实现。从属于“简计划”。
https://npmjs.com/package/@ltd/j-toml
GNU Lesser General Public License v3.0
55 stars 6 forks source link

Is there a simple demo code to provide pretty format? #25

Closed newproplus closed 1 year ago

newproplus commented 1 year ago

For example,my toml file like

[appearance]
custom_backaground_img = './custom_background_image.png'
custom_backaground_opacity = 0.44999998807907104
date_format = 'MM-dd-yyyy'
editor_theme = ''
language = 'zh-CHS'
theme = 'wheat'
time_format = 'HH:mm:ss '

[encryption]
enable_file_compress = true
entry_file_name = 'manifest.enc'
file_ext = '.enc'
master_password = ''

[normal]
show_file_saving_status = true
spell_check = false

[sync]
enable_fail_safe = true
interval_seconds = 5
max_concurrent_connections = 5
storage_type = 'aliyunOss'

[sync.aliyun_oss]
access_key_id = ''
access_key_secret = ''
bucket = ''
region = ''
url = ''

[sync.amazon_s3]
access_key = ''
bucket = ''
region = ''
secret_key = ''
url = ''

[sync.local_disk]
data_dir_path = ''

The output in console of parse is right,then I use stringify like :

    console.log(">>> toml :: ", stringifyToml(settingStore.setting, {
            newline: "\n",
            newlineAround: "section",
            xBeforeNewlineInMultilineTable: ","
        }))

The output will like:

normal.show_file_saving_status = true
normal.spell_check = false
appearance.language = ''
appearance.editor_theme = ''
appearance.date_format = ''
appearance.theme = ''
appearance.time_format = ''
appearance.custom_backaground_img = ''
appearance.custom_backaground_opacity = 0.8
sync.storage_type = ''
sync.amazon_s3.bucket = ''
sync.amazon_s3.url = ''
sync.amazon_s3.region = ''
sync.amazon_s3.access_key = ''
sync.amazon_s3.secret_key = ''
sync.aliyun_oss.bucket = ''
sync.aliyun_oss.url = ''
sync.aliyun_oss.region = ''
sync.aliyun_oss.access_key_id = ''
sync.aliyun_oss.access_key_secret = ''
sync.local_disk.data_dir_path = ''
sync.interval_seconds = 5.0
sync.enable_fail_safe = true
sync.max_concurrent_connections = 5.0
encryption.master_password = ''
encryption.entry_file_name = 'manifest.enc'
encryption.file_ext = '.enc'
encryption.enable_file_compress = true

How to make a pretty format?

Thank you

LongTengDao commented 1 year ago

It seems you need all tables be section?

You can mark the value before you stringify:

function markAllTableAsSection (value) {
    if ( Object.prototype.toString.call(value)==='[object Object]' && !TOML.isSection(value) ) {
        TOML.Section(value);
        Object.values(value).forEach(markAllTableAsSection);
    }
}

markAllTableAsSection(settingStore.setting);

BTW: if you parse the toml file by using parser of this lib, the parsed table will auto remember the style you written. Your settingStore is a third-party reactive copy?

newproplus commented 1 year ago

Thank you, It's the data stored by pinia, a vue3 project