Skip to content

Commit 5445b3c

Browse files
committed
feat: better display of not exist embed
1 parent 81e0d36 commit 5445b3c

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ Intended to be used by obsidian user.
3636
The plugin will create :
3737
- A link to the original file, with the form of : `<a href="original link" class="link_citation"><i class='fas fa-link'></i></a>`
3838
- A div with the founded content : `<div class="citation">content founded</div>`
39-
- In case of the link / contents is not found : `<div class="not_found">filename#part</div>`
39+
- In case of the link / contents is not found the following block is created instead :
40+
```html
41+
<div class='citation'><a class='link_citation'><i class='fas fa-link'></i></a><p style="text-align: center; display: block"><i class="not_found"> link_alt </i> {a configured message}</p></div>
42+
```
43+
The message for the not found file can be customized in `mkdocs.yml`. The default message is `file not exists`.
4044

4145
You can add a css in your `docs/assets/css` (or whatever the path is), and add it to your `mkdocs.yml` :
4246
```yml
@@ -65,6 +69,7 @@ plugins:
6569
- embed_file:
6670
callouts: true
6771
custom-attribute: 'assets/css/custom_attributes.css' //need to be the same as in the config!
72+
language_message: 'file not exists.'
6873
```
6974

7075
# Limitation

mkdocs_embed_file_plugins/plugin.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def strip_comments(markdown):
106106
markdown = re.sub(r'%%(.*)%%', '', markdown, flags=re.DOTALL)
107107
return markdown
108108

109-
def cite(md_link_path, link, soup, citation_part, config, callouts, custom_attr) -> BeautifulSoup:
109+
def cite(md_link_path, link, soup, citation_part, config, callouts, custom_attr, msg) -> BeautifulSoup:
110110
"""Append the content of the founded file to the original file.
111111
112112
Args:
@@ -138,8 +138,11 @@ def cite(md_link_path, link, soup, citation_part, config, callouts, custom_attr)
138138
contents = frontmatter.loads(text).content
139139
quote = search_in_file(citation_part, contents)
140140
tooltip_template = (
141-
"<div class='not_found'>" +
142-
str(unquote(link['alt'].replace('/', ''))) + '</div>'
141+
"<div class='citation'> <a href='"
142+
+ str(link['src'])
143+
+ "' class='link_citation'><i class='fas fa-link'></i> </a>"
144+
+ str(link['alt']) + ' not exists.'
145+
+ '</div>'
143146
)
144147
if len(quote) > 0:
145148
if callouts:
@@ -178,20 +181,19 @@ def cite(md_link_path, link, soup, citation_part, config, callouts, custom_attr)
178181
)
179182
+ '</div>'
180183
)
184+
new_soup = str(soup).replace(str(link), str(tooltip_template))
185+
soup = BeautifulSoup(new_soup, 'html.parser')
186+
return soup
181187
else:
182188
print('**** Citation not found **** : ' + unquote(citation_part), 'for : ', str(md_link_path), ' with link: ' + str(link) + ' and new_uri: ' + str(new_uri), ' and quote: ' + str(quote))
183-
tooltip_template = (
184-
"<div class='not_found'>" +
185-
str(unquote(link['alt'].replace('/', ''))) + '</div>'
186-
)
187-
new_soup = str(soup).replace(str(link), str(tooltip_template))
188-
soup = BeautifulSoup(new_soup, 'html.parser')
189-
return soup
189+
return tooltip_not_found(link, soup, msg)
190+
190191

191-
def tooltip_not_found(link, soup) -> BeautifulSoup:
192+
def tooltip_not_found(link, soup, msg) -> BeautifulSoup:
192193
tooltip_template = (
193-
"<div class='not_found'>" +
194-
str(unquote(link['alt'].replace('/', ''))) + '</div>'
194+
"<div class='citation'> <a class='link_citation'><i class='fas fa-link'></i> </a>"
195+
+ '<p style="text-align: center; display: block"><i class="not_found">' + str(link['alt']) + f'</i> {msg}</p>'
196+
+ '</div>'
195197
)
196198
new_soup = str(soup).replace(str(link), str(tooltip_template))
197199
soup = BeautifulSoup(new_soup, 'html.parser')
@@ -215,7 +217,8 @@ def search_file_in_documentation(link: Union[Path,str], config_dir: Path) -> Uni
215217
class EmbedFile(BasePlugin):
216218
config_scheme = (
217219
('callouts', config_options.Type(bool, default=False)),
218-
('custom-attributes', config_options.Type(str, default=''))
220+
('custom-attributes', config_options.Type(str, default='')),
221+
('language_message', config_options.Type(str, default='file not exists')),
219222
)
220223

221224
def __init__(self):
@@ -227,6 +230,7 @@ def on_post_page(self, output_content, page, config) -> str:
227230
docs = Path(config['docs_dir'])
228231
md_link_path = ''
229232
callout = self.config['callouts']
233+
language_message = self.config['language_message']
230234
for link in soup.findAll(
231235
'img',
232236
src=lambda src: src is not None
@@ -270,7 +274,7 @@ def on_post_page(self, output_content, page, config) -> str:
270274
)
271275
md_link_path = Path(unquote(md_link_path)).resolve()
272276
if (md_link_path == 0):
273-
soup = tooltip_not_found(link, soup)
277+
soup = tooltip_not_found(link, soup, language_message)
274278
if (md_link_path != '' or md_link_path == 0) and len(link['src']) > 0:
275279
if '#' in link.get('alt', ''):
276280
# heading
@@ -284,10 +288,10 @@ def on_post_page(self, output_content, page, config) -> str:
284288

285289
if os.path.isfile(md_link_path):
286290
soup = cite(md_link_path, link, soup,
287-
citation_part, config, callout, self.config['custom-attributes'])
291+
citation_part, config, callout, self.config['custom-attributes'], language_message)
288292
else:
289293
link_found=search_file_in_documentation(md_link_path, docs)
290294
if link_found != 0:
291295
soup = cite(link_found, link, soup,
292-
citation_part, config, callout, self.config['custom-attributes'])
296+
citation_part, config, callout, self.config['custom-attributes'], language_message)
293297
return str(soup)

0 commit comments

Comments
 (0)