-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support hidden code lines #254
feat: support hidden code lines #254
Conversation
…line, instead of all occurrences
…de shell execution tests
e9259eb
to
f9bdfc0
Compare
From #242 (comment):
Totally makes sense @mfontanini - I can make those changes. So is the thinking that from |
I'd imagine that not all of the supported languages definitely have some equivalent for this prefix in their own docs' syntax. Would we just decide on some sensible prefix in those cases, should the need arise? |
Yeah I'd fix those as they come. I imagine in most cases, rust being an exception, the hidden line prefix would be an extension to the language's comment syntax. So e.g. for python/shell script it could be |
src/markdown/elements.rs
Outdated
pub(crate) fn hidden_line_prefix(&self) -> Option<&'static str> { | ||
match self { | ||
CodeLanguage::Rust => Some("# "), | ||
CodeLanguage::Python | CodeLanguage::Shell(_) | CodeLanguage::Bash => Some("## "), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there is a better prefix over to use for these languages since a single #
denotes a comment in them, so if you wanted to show comments in the snippet, that could potentially be confusing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using ///
? I only dislike that in rust because it has a meaning but for the rest of the languages that could work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, can roll with ///
.
examples/hidden_code_lines.md
Outdated
@@ -0,0 +1,111 @@ | |||
--- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This presentation was primarily for demonstration purposes (especially around the line numbering and highlighting). Let me know if you think I should remove/amend it @mfontanini.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great but for now I'd like to remove it. I usually write docs about new features along when I'm about to release the next version, and I'll use some of these examples there. I'm planning on, besides having docs for this (and snippet code execution in general, touching the examples/code.md
example presentation to include more cases like these ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
I'm starting to have second thoughts about this change, specifically from the security angle. With this change you could run a code snippet in a presentation and it could do something like an Thoughts? Am I too paranoid? |
A good point. I think the risk of executing malicious code is present regardless of hidden code lines. All it would take is a user to execute code in a presentation without actually inspecting/understanding: the snippet as it is seen in the presentation, the markdown file, or any local executors. Even without hidden code lines and local executors, there has to be some level of trust that a shared/downloaded presentation does not contain malicious code snippets. Of course, hidden code lines/local executor scripts could elevate this risk - if users rely only on viewing the presentation output and do not inspect the sources. Indeed, other tools like But perhaps we could add some sort of safeguard around this for hidden code lines - maybe a CLI flag |
Yeah, definitely, but hidden code lines make this a lot more dangerous.
I agree, if this was implemented we'd need this. However, then it would become annoying for the user that's building a presentation. Now, we could put this as a config option in the config file but this ends up defeating the purpose with foreign presentations. I have mixed feelings about this :/ |
Fair enough @mfontanini. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @dmackdev, I'm reviving this! I recently added a configuration/CLI arg that you need to use to explicitly enable snippet code execution in #276. Since that was my main concern and it's now there, I'd like to get this PR in as it adds a lot of value.
I left a couple of comments and it looks like there's some conflicts as well.
src/markdown/elements.rs
Outdated
let mut line = format!("{}\n", line); | ||
if line.starts_with(prefix) { | ||
line.replace_range(..prefix.len(), ""); | ||
} | ||
line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can replace all of this with something like
let line = line.strip_prefix(prefix).unwrap_or(&line);
format!("{line}\n")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one! I have made that change, but also updated it to use fold
instead of map
as per the Clippy suggestion.
examples/hidden_code_lines.md
Outdated
@@ -0,0 +1,111 @@ | |||
--- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great but for now I'd like to remove it. I usually write docs about new features along when I'm about to release the next version, and I'll use some of these examples there. I'm planning on, besides having docs for this (and snippet code execution in general, touching the examples/code.md
example presentation to include more cases like these ones.
src/markdown/elements.rs
Outdated
pub(crate) fn hidden_line_prefix(&self) -> Option<&'static str> { | ||
match self { | ||
CodeLanguage::Rust => Some("# "), | ||
CodeLanguage::Python | CodeLanguage::Shell(_) | CodeLanguage::Bash => Some("## "), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using ///
? I only dislike that in rust because it has a meaning but for the rest of the languages that could work.
Thanks, this is great! |
This PR adds support for hidden code lines for the languages Rust, Python, Shell and Bash using prefixes at the start of the line, particular to each language. Support for more languages can be added in future.
The hidden code lines will not be visible in snippets in the presentation, but still be evaluated as normal if executed.
See the example presentation for demonstration.