Skip to content

Commit

Permalink
Merge pull request #69 from anyproto/go-1153-random-icons-in-markdown…
Browse files Browse the repository at this point in the history
…-import

GO-1153: random icons in all imports
  • Loading branch information
AnastasiaShemyakinskaya authored Jun 7, 2023
2 parents d6caf86 + 642c295 commit 36854f1
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 37 deletions.
2 changes: 1 addition & 1 deletion core/block/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions core/block/import/converter/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}
}
Expand Down
2 changes: 1 addition & 1 deletion core/block/import/csv/collectionstrategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/block/import/csv/tablestrategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion core/block/import/html/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()},
}

Expand Down
28 changes: 4 additions & 24 deletions core/block/import/markdown/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions core/block/import/notion/api/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"context"
"strings"
"time"

"github.com/globalsign/mgo/bson"
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion core/block/import/txt/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()},
}

Expand Down

0 comments on commit 36854f1

Please sign in to comment.