diff --git a/core/block/export/export.go b/core/block/export/export.go index 0a75eb1881..f42e51b174 100644 --- a/core/block/export/export.go +++ b/core/block/export/export.go @@ -230,7 +230,7 @@ func (e *export) getNested(id string, docs map[string]*types.Struct) { if !validType(sbt) { continue } - rec, qErr := e.objectStore.QueryById(links) + rec, qErr := e.objectStore.QueryById([]string{link}) if qErr != nil { log.Errorf("failed to query id %s, err: %s", qErr, err.Error()) continue diff --git a/core/block/import/converter/common.go b/core/block/import/converter/common.go index 94d5edd773..7fcd7fc2f7 100644 --- a/core/block/import/converter/common.go +++ b/core/block/import/converter/common.go @@ -22,8 +22,11 @@ import ( "github.com/anyproto/anytype-heart/pkg/lib/logging" "github.com/anyproto/anytype-heart/pkg/lib/pb/model" "github.com/anyproto/anytype-heart/util/pbtypes" + "github.com/anyproto/anytype-heart/util/slice" ) +var randomIcons = []string{"📓", "📕", "📗", "📘", "📙", "📖", "📔", "📒", "📝", "📄", "📑"} + var log = logging.Logger("import") func GetSourceDetail(fileName, importPath string) string { @@ -36,16 +39,17 @@ func GetSourceDetail(fileName, importPath string) string { return source.String() } -func GetDetails(name string) *types.Struct { - var title string - - if title == "" { - title = strings.TrimSuffix(filepath.Base(name), filepath.Ext(name)) +func GetCommonDetails(sourcePath, name, emoji string) *types.Struct { + if name == "" { + name = strings.TrimSuffix(filepath.Base(sourcePath), filepath.Ext(sourcePath)) + } + if emoji == "" { + emoji = slice.GetRandomString(randomIcons, name) } - fields := map[string]*types.Value{ - bundle.RelationKeyName.String(): pbtypes.String(title), - bundle.RelationKeySourceFilePath.String(): pbtypes.String(name), + bundle.RelationKeyName.String(): pbtypes.String(name), + bundle.RelationKeySourceFilePath.String(): pbtypes.String(sourcePath), + bundle.RelationKeyIconEmoji.String(): pbtypes.String(emoji), } return &types.Struct{Fields: fields} } diff --git a/core/block/import/csv/collectionstrategy.go b/core/block/import/csv/collectionstrategy.go index aeba1ec4cb..16328fcc97 100644 --- a/core/block/import/csv/collectionstrategy.go +++ b/core/block/import/csv/collectionstrategy.go @@ -35,7 +35,7 @@ func NewCollectionStrategy(collectionService *collection.Service) *CollectionStr func (c *CollectionStrategy) CreateObjects(path string, csvTable [][]string) ([]string, []*converter.Snapshot, error) { snapshots := make([]*converter.Snapshot, 0) allObjectsIDs := make([]string, 0) - details := converter.GetDetails(path) + details := converter.GetCommonDetails(path, "", "") details.GetFields()[bundle.RelationKeyLayout.String()] = pbtypes.Float64(float64(model.ObjectType_collection)) _, _, st, err := c.collectionService.CreateCollection(details, nil) if err != nil { diff --git a/core/block/import/csv/tablestrategy.go b/core/block/import/csv/tablestrategy.go index bcb3209e03..e4bbc288e3 100644 --- a/core/block/import/csv/tablestrategy.go +++ b/core/block/import/csv/tablestrategy.go @@ -37,7 +37,7 @@ func (c *TableStrategy) CreateObjects(path string, csvTable [][]string) ([]strin } } - details := converter.GetDetails(path) + details := converter.GetCommonDetails(path, "", "") sn := &model.SmartBlockSnapshotBase{ Blocks: st.Blocks(), Details: details, diff --git a/core/block/import/html/converter.go b/core/block/import/html/converter.go index 13af1be40b..d7bda02b5f 100644 --- a/core/block/import/html/converter.go +++ b/core/block/import/html/converter.go @@ -154,7 +154,7 @@ func (h *HTML) getBlocksForFile(rc io.ReadCloser) ([]*model.Block, error) { func (h *HTML) getSnapshot(blocks []*model.Block, p string) (*converter.Snapshot, string) { sn := &model.SmartBlockSnapshotBase{ Blocks: blocks, - Details: converter.GetDetails(p), + Details: converter.GetCommonDetails(p, "", ""), ObjectTypes: []string{bundle.TypeKeyPage.URL()}, } diff --git a/core/block/import/markdown/import.go b/core/block/import/markdown/import.go index c97a317358..0f3d8ff293 100644 --- a/core/block/import/markdown/import.go +++ b/core/block/import/markdown/import.go @@ -21,14 +21,11 @@ import ( "github.com/anyproto/anytype-heart/pkg/lib/logging" "github.com/anyproto/anytype-heart/pkg/lib/pb/model" "github.com/anyproto/anytype-heart/util/pbtypes" - "github.com/anyproto/anytype-heart/util/slice" ) var ( emojiAproxRegexp = regexp.MustCompile(`[\x{2194}-\x{329F}\x{1F000}-\x{1FADF}]`) - - log = logging.Logger("markdown-import") - articleIcons = []string{"📓", "📕", "📗", "📘", "📙", "📖", "📔", "📒", "📝", "📄", "📑"} + log = logging.Logger("markdown-import") ) const numberOfStages = 9 // 8 cycles to get snaphots and 1 cycle to create objects @@ -557,30 +554,13 @@ func (m *Markdown) setNewID( return nil } -func (m *Markdown) setDetails(file *FileInfo, name string, details map[string]*types.Struct) { +func (m *Markdown) setDetails(file *FileInfo, fileName string, details map[string]*types.Struct) { var title, emoji string if len(file.ParsedBlocks) > 0 { title, emoji = m.extractTitleAndEmojiFromBlock(file) } - - if emoji == "" { - emoji = slice.GetRandomString(articleIcons, name) - } - - if title == "" { - title = strings.TrimSuffix(filepath.Base(name), filepath.Ext(name)) - titleParts := strings.Split(title, " ") - title = strings.Join(titleParts[:len(titleParts)-1], " ") - } - - file.Title = title - // FIELD-BLOCK - fields := map[string]*types.Value{ - bundle.RelationKeyName.String(): pbtypes.String(title), - bundle.RelationKeyIconEmoji.String(): pbtypes.String(emoji), - bundle.RelationKeySourceFilePath.String(): pbtypes.String(file.Source), - } - details[name] = &types.Struct{Fields: fields} + details[fileName] = converter.GetCommonDetails(fileName, title, emoji) + file.Title = pbtypes.GetString(details[fileName], bundle.RelationKeyName.String()) } func (m *Markdown) extractTitleAndEmojiFromBlock(file *FileInfo) (string, string) { diff --git a/core/block/import/notion/api/database/database.go b/core/block/import/notion/api/database/database.go index 6a9d0de5d4..58a9e99392 100644 --- a/core/block/import/notion/api/database/database.go +++ b/core/block/import/notion/api/database/database.go @@ -2,6 +2,7 @@ package database import ( "context" + "strings" "time" "github.com/globalsign/mgo/bson" @@ -114,6 +115,9 @@ func (ds *Service) makeDatabaseSnapshot(d Database, } detailsStruct = pbtypes.StructMerge(st.CombinedDetails(), detailsStruct, false) snapshots := make([]*converter.Snapshot, 0) + if snapshot := ds.handleNameProperty(d, request, st); snapshot != nil { + snapshots = append(snapshots, snapshot) + } for key, databaseProperty := range d.Properties { if snapshot := ds.createRelationFromDatabaseProperty(request, databaseProperty, key, st); snapshot != nil { snapshots = append(snapshots, snapshot) @@ -126,6 +130,28 @@ func (ds *Service) makeDatabaseSnapshot(d Database, return snapshots, nil } +func (ds *Service) handleNameProperty(d Database, request *block.MapRequest, st *state.State) *converter.Snapshot { + var ( + snapshot *converter.Snapshot + key = "Name" + nameProperty property.DatabasePropertyHandler + ok bool + ) + if nameProperty, ok = d.Properties[key]; !ok { + if nameProperty, ok = d.Properties[strings.ToUpper(key)]; ok { + key = strings.ToUpper(key) + } else if nameProperty, ok = d.Properties[strings.ToLower(key)]; ok { + key = strings.ToLower(key) + } + } + if nameProperty == nil { + return nil + } + snapshot = ds.createRelationFromDatabaseProperty(request, nameProperty, key, st) + delete(d.Properties, key) + return snapshot +} + func (ds *Service) createRelationFromDatabaseProperty(req *block.MapRequest, databaseProperty property.DatabasePropertyHandler, key string, diff --git a/core/block/import/txt/converter.go b/core/block/import/txt/converter.go index de630f4279..5536ae9ca2 100644 --- a/core/block/import/txt/converter.go +++ b/core/block/import/txt/converter.go @@ -149,7 +149,7 @@ func (t *TXT) getBlocksForFile(rc io.ReadCloser) ([]*model.Block, error) { func (t *TXT) getSnapshot(blocks []*model.Block, p string) (*converter.Snapshot, string) { sn := &model.SmartBlockSnapshotBase{ Blocks: blocks, - Details: converter.GetDetails(p), + Details: converter.GetCommonDetails(p, "", ""), ObjectTypes: []string{bundle.TypeKeyPage.URL()}, }