Comments (20)
So you want to truncate, but by words, right?
from gostatic.
Yes. But I want to mantain the syntax of the markdown. If I truncate the raw code, then perhaps I get broken url: [foo
instead of [foo](bla)
. Or broken emph, for example *foo
instead of *foo*
.
The same applies to html code.
from gostatic.
Yeah, I don't have any good solutions for this right now. That needs to be context-aware truncater...
from gostatic.
But how wordpress themes do that? (see example above)
from gostatic.
I'm quite sure they do some fancy processing in there. Saying 'I don't have any good solutions` I mean that I don't really have time to sit and code an algorithm which will do that. I would gladly accept pull requests implementing that, though, since it sounds quite useful.
On the other hand, I looked at what people do, and they use simpler approach: you render your content, strip tags and then truncate. This way you won't have any markup truncated in a middle.
from gostatic.
OK. Looking it up, I found this which is simple but very capable. There is a explaining post
from gostatic.
The best way is to run
{{ truncate (pandoc --to plain .Raw) }}
but for this I need a new release version with #51 . I reopen this in waiting for that.
from gostatic.
I'd say best way would be to do {{ .Raw | markdown | strip_html | truncate 300 }}
, this way you're not calling out to shell and have no external dependencies.
from gostatic.
Is it a viable option to introduce a template function, e.g. wordify
or whatever which does that logic?
from gostatic.
Could be, but is markdown|strip|truncate not enough?
from gostatic.
Yeah I guess, but it doesn't break on words, but on characters, right?
from gostatic.
Right, function for breaking on words would be nice to have. Not sure wordify
is good name for that though. :)
from gostatic.
👍 perhaps excerpt
is a good name
from gostatic.
Sounds good to me!
from gostatic.
So, could anyone implement this? I have no this level to do that.
from gostatic.
It's in pull request #56 .
from gostatic.
@krpors : fantastic. Is this tested against markdown syntax. I'm thinking about *foo*
or [goo](google.com)
.
from gostatic.
@somenxavier Er, no. That requires a whole new level of parsing and things... I guess I misinterpreted your requirement. What the excerpt
function does however, is truncating text based on spaces so it doesn't just break of in the middle of a word. If you now chain the function as follows:
{{ excerpt (.Raw | markdown | strip_html) 7 }}
It will produce simply text, maximum of 7 words. You can maybe play a bit without strip_html
to see if you can produce correct results. If not, we may have to look for another solution.
from gostatic.
@krpors I mean that if you write
Hi. This is a *test*. Do you see this [web](google.com)
the the excerpt could produce something like:
Hi, This is a test. Do you see this web
But your solution is good enough
from gostatic.
That is correct. The function chain will parse the markdown of your page, strip the html from it (so only text remains), and then it will generate an excerpt out of it.
from gostatic.
Related Issues (20)
- Inconsistencies with gostatic.Page HOT 2
- Globstar support
- Html formating in site.gohtml can trigger <pre><code> output HOT 1
- 2021-02-06-somepost.md style filenames date parsing
- Generate tag overview on 1 page HOT 2
- Relativize filter destroys protocolless URLs HOT 1
- Template bug? HOT 3
- Interest in Chroma highlighting? HOT 1
- ~30 golangci-lint warnings on codebase HOT 1
- 404 error handling? HOT 1
- UTF-8 with BOM support HOT 2
- Multiple outputs? HOT 3
- Question: how to you relativize paths? HOT 2
- [question] Use Date field for directory structure
- Large memory usage when downloading large files HOT 6
- [question] Config file name and file format HOT 1
- [yaml] support CRLF HOT 1
- [yaml] tags not present
- dump option HOT 1
- download script HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gostatic.