def init(): config_dir = os.getcwd() config_filename = f".{app.info.name}.yml" config_path: Path = Path(config_dir) / config_filename typer.echo( f"👋 Welcome in {app.info.name}, we are going to edit your {config_filename}" ) understand_wikilinks = typer.confirm( "Should it understand wiki-links (example `[[Label]]`):", default=True) reference_by = typer.prompt( "Should the title or the filename be considered as the unique key to reference a note ?", type=Choice(["title", "filename"], case_sensitive=False), show_choices=True, default="filename", ) link_system = typer.prompt( "What is your link system links or wiki-links ?", type=Choice(["wikilink", "link"], case_sensitive=False), show_choices=True, default="link", ) init_config = LinkyNoteConfig( parse_config=ParseConfig(parse_wikilinks=understand_wikilinks), modify_config=ModifyConfig(reference_by=reference_by, link_system=link_system), ) Config.write(config_path, init_config)
def read_references( parsed_files: Dict[NotePath, Document]) -> Dict[Note, Document]: _reference_db = references_db.REFERENCE_DB_FACTORY(ModifyConfig()) rv = {} for filename, ast in parsed_files.items(): note, references = reference_extractor.EXTRACTOR_FACTORY( filename).extract_references(ast) rv[note] = ast db_response = _reference_db.upsert_note( references_db.UpsertNoteQuery(note=note)) for reference in references: _insert_target_note_and_reference(_reference_db, db_response, reference) return rv
def test_marko_modifier_link_system_url_encode(build_ast, mocked_db): # Given # - a reference whose filename needs to be url encode # - a reference whose filename is already url endoded source_note = Note( note_title=NoteTitle("Marketing"), note_path=NotePath("Marketing.md") ) url = f"[{source_note.note_title}]({source_note.note_path})" ast = build_ast(source_note) returned_value = GetReferencesResponse( ( Reference( source_note=Note( note_title=NoteTitle("Digital Marketing"), note_path=NotePath("digital marketing.md"), ), target_note=source_note, context=ReferenceContext(f"A reference to {url}"), ), Reference( source_note=Note( note_title=NoteTitle("Content Marketing"), note_path=NotePath("content%20marketing.md"), ), target_note=source_note, context=ReferenceContext(f"A reference to {url}"), ), ) ) modifier = MarkoModifierImpl(mocked_db(returned_value), ModifyConfig()) # When # - Modifying the ast modified_ast = modifier.modify_ast(ast, source_note) # Then # - The link to the reference is URL encoded # - an already url encoded link is not re-encoded assert ( modified_ast.children[10].children[0].children[0].children[0].dest == "digital%20marketing.md" ) assert ( modified_ast.children[10].children[1].children[0].children[0].dest == "content%20marketing.md" )
def test_marko_modifier_nominal_wikilink_system(build_ast, mocked_db): # Given source_note = Note( note_title=NoteTitle("Marketing"), note_path=NotePath("Marketing.md") ) url = f"[{source_note.note_title}]({source_note.note_path})" ast = build_ast(source_note) returned_value = GetReferencesResponse( ( Reference( source_note=Note( note_title=NoteTitle("Digital Marketing"), note_path=NotePath("digital-marketing.md"), ), target_note=source_note, context=ReferenceContext(f"A reference to {url}"), ), Reference( source_note=Note( note_title=NoteTitle("Digital Marketing"), note_path=NotePath("digital-marketing.md"), ), target_note=source_note, context=ReferenceContext(f"Another reference to {url}"), ), Reference( source_note=Note( note_title=NoteTitle("No Marketing"), note_path=NotePath("No-marketing.md"), ), target_note=source_note, context=ReferenceContext(f"A reference to {url}"), ), ) ) modifier = MarkoModifierImpl( mocked_db(returned_value), ModifyConfig(link_system=LinkSystem.WIKILINK) ) # When modified_ast = modifier.modify_ast(ast, source_note) # Then assert isinstance(modified_ast.children[3].children[1], RawText)
def _config_from_dict(config_dict: Dict[str, Any]) -> LinkyNoteConfig: parse_config = ParseConfig( parse_wikilinks=config_dict.get( "parse_config", ParseConfig.parse_wikilinks ), ) modify_config = ModifyConfig( link_system=LinkSystem( config_dict.get( "modify_config", {"link_system": ModifyConfig.link_system} )["link_system"] ), reference_by=ReferenceBy( config_dict.get( "modify_config", {"reference_by": ModifyConfig.reference_by} )["reference_by"] ), ) return LinkyNoteConfig( parse_config=parse_config, modify_config=modify_config )
def test_db(): return factory(modify_config=ModifyConfig())